下一个按钮不显示Wordpress中的其他帖子


Next button not showing other posts in Wordpress

在我的wordpress中,我有很多帖子,每个页面我都显示5个帖子。在我的页面底部,我有下一个和上一个按钮。当我点击下一个按钮,它会去/page/2/链接,但这个页面标题显示Page not found。并且在第2页没有显示其他文章

我的下一个和上一个代码:

  <div class="prev-next-btn">
             <?php next_posts_link( __( 'next', 'themename' ) ); ?>
            </div>
            <div class="prev-next-btn">
                <?php previous_posts_link( __( 'prev', 'themename' ) ); ?>
            </div>

我的index.php代码:

   <div class="center-content">
        <ul>
            <?php query_posts('posts_per_page=5'); ?>
            <?php   if (have_posts()) : while (have_posts()) : the_post(); ?>
          <li>
            <div class="date">In
                <?php
                    $category = get_the_category(); 
                    if(!empty($category))
                        echo '<a href="'.get_category_link($category[0]->term_id ).'">'.$category[0]->cat_name.'</a>';
                    ?> 
                    - <?php the_time('d M, Y') ?>
          </div>
            <!--<div class="auther">by <?php the_author(); ?>  <span> - <?php comments_number( 'no comments', 'one comment', '% comments' ); ?> Reply</span></div>-->
            <div class="title clear-both"><h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2></div>
            <div class="details"><p><?php the_excerpt(); ?></p></div>
            <div class="readmore"><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">Read more</a></div>
            <br>
          </li>
            <?php endwhile; ?>
        </ul>
        </div>
          <div class="pagination">
                <?php
                    if(function_exists('wp_pagenavi')) { 
                        wp_pagenavi(); 
                    }
                    else {
                ?>  
            <div class="prev-next-btn">
             <?php next_posts_link( __( 'next', 'themename' ) ); ?>
            </div>
            <div class="prev-next-btn">
                <?php previous_posts_link( __( 'prev', 'themename' ) ); ?>
            </div>
              <?php } ?>
          </div>
      <?php else : ?>
            404 Nothing here. Sorry.
            <?php endif; ?>
      </div>

你确实有一些问题

  1. 永远不要使用query_posts。它很慢,重新运行查询,分页中断和静默失败,更糟糕的是,它破坏了主查询对象。如果中断了主查询对象,则中断了页面函数。因此,请不要使用query_posts

  2. 你已经用自定义查询替换了主查询循环,这是你不能做的。如果您需要在特定页面上显示不同数量的帖子(而不是在页面模板和静态首页上,因为这不会在那里工作),那么使用pre_get_posts。你需要去阅读这个动作有多有用以及如何使用它

删除这一行

<?php query_posts('posts_per_page=5'); ?>

然后在函数文件

中添加以下内容
add_action( 'pre_get_posts', function ( $query )
{
    if ( $query->is_home() && $query->is_main_query() ) {
      $query->set( 'posts_per_page', 5 );
    }
});
<?php
// set the "paged" parameter (use 'page' if the query is on a static front page)
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
// the query
$the_query = new WP_Query( 'cat=1&paged=' . $paged ); 
?>
<?php if ( $the_query->have_posts() ) : ?>
<?php
// the loop
while ( $the_query->have_posts() ) : $the_query->the_post(); 
?>
<?php the_title(); ?>
<?php endwhile; ?>
<?php
// next_posts_link() usage with max_num_pages
next_posts_link( 'Next', $the_query->max_num_pages );
previous_posts_link( 'Previous' );
?>
<?php 
// clean up after the query and pagination
wp_reset_postdata(); 
?>
<?php endif; ?>

只需添加下面的代码:我添加了'page '变量并设置在next_posts_link()previous_posts_link() ..请参阅下面的代码:

<div class="center-content">
        <ul>
            <?php 
            $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;   
            query_posts(array('posts_per_page'=>5,'paged'=>$paged )); ?>
            <?php   if (have_posts()) : while (have_posts()) :the_post(); ?>
          <li>
            <div class="date">In
                <?php
                    $category = get_the_category(); 
                    if(!empty($category))
                        echo '<a href="'.get_category_link($category[0]->term_id ).'">'.$category[0]->cat_name.'</a>';
                    ?> 
                    - <?php the_time('d M, Y') ?>
          </div>
            <!--<div class="auther">by <?php the_author(); ?>  <span> - <?php comments_number( 'no comments', 'one comment', '% comments' ); ?> Reply</span></div>-->
            <div class="title clear-both"><h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2></div>
            <div class="details"><p><?php the_excerpt(); ?></p></div>
            <div class="readmore"><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">Read more</a></div>
            <br>
          </li>
            <?php endwhile; ?>
        </ul>
        </div>
          <div class="pagination">
                <?php
                global $wp_query;
                    if(function_exists('wp_pagenavi')) { 
                        wp_pagenavi(); 
                    }
                    else { echo 'test';
                ?>  
            <div class="prev-next-btn">
             <?php echo next_posts_link( __( 'next', 'themename' ) , $wp_query->max_num_pages ); ?>
            </div>
            <div class="prev-next-btn">
                <?php echo previous_posts_link( __( 'prev', 'themename' ) , $wp_query->max_num_pages ); ?>
            </div>
              <?php } ?>
          </div>
      <?php else : ?>
            404 Nothing here. Sorry.
            <?php endif; ?>
      </div>