主页中较旧条目的问题

时间:2016-07-18 作者:Wha2wear

当我点击主页上的“旧条目”时,它会转到第二个链接,但显示的是相同的帖子。我想我需要修改index.php, 这是我认为需要修改的完整代码部分:

<div class="container">
    <div class="post_content">
        <div class="home_posts">
            <?php
                $args2 = array(
                \'post_type\' => \'post\',
                \'posts_per_page\' => 10,
                \'paged\' => ( get_query_var(\'paged\') ? get_query_var(\'paged\') : 2),
                );
                $query = new WP_Query( $args2 );
                if ( $query->have_posts() ) :
                    while ( $query->have_posts() ) : $query->the_post();
                        echo \'<div class="grid_post">
                                <h3><a href="\'.get_permalink().\'">\'.get_the_title().\'</a></h3>\';
                        $type = get_post_meta($post->ID,\'page_featured_type\',true);
                        switch ($type) {
                            case \'youtube\':
                                echo \'<iframe width="560" height="315" src="http://www.youtube.com/embed/\'.get_post_meta( get_the_ID(), \'page_video_id\', true ).\'?wmode=transparent" frameborder="0" allowfullscreen></iframe>\';
                                break;
                            case \'vimeo\':
                                echo \'<iframe src="http://player.vimeo.com/video/\'.get_post_meta( get_the_ID(), \'page_video_id\', true ).\'?title=0&amp;byline=0&amp;portrait=0&amp;color=03b3fc" width="500" height="338" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>\';
                                break;
                            default:
                                echo \'<div class="grid_post_img">
                                            <a href="\'.get_permalink().\'">\'.get_the_post_thumbnail().\'</a>
                                        </div>\';
                                break;
                        }
                        echo \'<div class="grid_home_posts">
                                    <p>\'.dess_get_excerpt(120).\'</p>
                                </div>
                            </div>
                            \';
                    endwhile;
            ?>
    </div>
    <?php
        echo \'<div class="load_more_content"><div class="load_more_text">\';
                ob_start();
                    next_posts_link(\'LOAD MORE\',$query->max_num_pages);
                    $buffer = ob_get_contents();
                ob_end_clean();
                if(!empty($buffer)) echo $buffer;
            echo\'</div></div>\';                 
        $max_pages = $query->max_num_pages;
            wp_reset_postdata();
        endif;
    ?>
    <span id="max-pages" style="display:none"><?php echo $max_pages ?></span>
    </div>
    <?php get_sidebar(); ?>
    <div class="clear"></div>
</div>
</div>
可能在这里:

<?php


$args2 = array(
    \'post_type\' => \'post\',
    \'posts_per_page\' => 6,
    \'paged\' => ( get_query_var(\'paged\') ? get_query_var(\'paged\') : 1),
    );
    $query = new WP_Query( $args2 );
    if ( $query->have_posts() ) :
        while ( $query->have_posts() ) : $query->the_post();
            echo \'<div class="grid_post">
这是“旧条目”的URL:http://www.wha2wear.com/page/2/ 显示与主页相同的内容。此外,我真的看不到代码中的某个地方写了“旧条目”。。

另外,在实际页面、头版中,有以下代码

[posts-for-page order_by =\'date\' hide_images=\'false\' num=\'11\' read_more=\'
Read More »\' show_full_posts=\'false\' use_wp_excerpt=\'true\' strip_html=\'true\' hide_post_content=\'false\' show_meta=\'false\' force_image_height=\'200\' force_image_width=\'250\']

I have the feeling that this code has higher priority than the php, so the homepage does what it says there?

3 个回复
SO网友:mlimon

为什么不尝试使用默认查询并像这样使用

if ( get_query_var(\'paged\') ) { $paged = get_query_var(\'paged\'); }
elseif ( get_query_var(\'page\') ) { $paged = get_query_var(\'page\'); }
else { $paged = 1; }

$args = array(
\'post_type\'         => \'post\',
\'posts_per_page\'    => 6,
\'paged\'             => $paged,
);

query_posts($args); while (have_posts()): the_post();

// do something

endwhile;
请参阅此处有关将“paged”参数添加到查询的更多详细信息

https://codex.wordpress.org/Pagination#Adding_the_.22paged.22_parameter_to_a_query

SO网友:majick

如果帖子是通过静态首页显示的(它们似乎是?)然后需要使用查询参数pagepaged. 您可以尝试以下操作:

$args2 = array(
    \'post_type\' => \'post\',
    \'posts_per_page\' => 6,
    \'paged\' => ( get_query_var(\'page\') ? get_query_var(\'page\') : 1),
);
如上所述:https://codex.wordpress.org/Function_Reference/get_query_var

编辑:但请注意paged 参数仍在查询数组中设置,但page 使用query\\u var。示例如下:https://codex.wordpress.org/Function_Reference/WP_Query

SO网友:ma_dev_15

$paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1;
$args=array(
    \'orderby\'=> \'id\',
    \'order\' => \'desc\',
    \'paged\'=>$paged
  );
global $wp_query;
$original_query = $wp_query;
$wp_query = null;
$wp_query = new WP_Query( $args );
  if ( have_posts() ) : 

    while ( have_posts() ):
       the_post();  


      get_template_part( \'template-parts/content\', get_post_format());
      endif;

      $count++;
      endwhile;
      // If no content, include the "No posts found" template.
    else :
        get_template_part( \'template-parts/content\', \'none\' );

    endif;

         $nav = get_the_posts_pagination( array(
            \'mid_size\'           => 4,
            \'prev_text\'          => __( \'&laquo;\', \'twentysixteen\' ),
            \'next_text\'          => __( \'&raquo;\', \'twentysixteen\' ),
            \'before_page_number\' => \'<span class="meta-nav screen-reader-text">\' . __( \'\', \'twentysixteen\' ) . \' </span>\',
            \'screen_reader_text\' => __( \'Post Navigation\' )
        ) );

         $nav = str_replace(\'<h2 class="screen-reader-text">Post Navigation</h2>\', \'\', $nav);
         echo $nav;

    $wp_query = null;
    $wp_query = $original_query;
    wp_reset_postdata();$wp_query = null;
    $wp_query = $original_query;
    wp_reset_postdata();
这就是它的工作方式,您需要更新WP\\u查询,而不是使用另一个对象,它不能用于分页。

相关推荐

Why my pagination gets 404?

我试图理解为什么我的分页从第二页开始,每页都会出现404错误。现在在我设置的阅读设置中Blog pages show at most 至1。<?php $paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1; $query = new WP_Query( array( \'post_type\' => \'behold_t