How can I fix my pagination?

时间:2016-06-30 作者:Chris Short

我正在尝试对自定义wp\\U查询post循环进行分页。我目前已经设置好了,但当我点击第2页时,它显示的是与第1页完全相同的帖子。我总共有大约8篇帖子,限制为每页5篇。下面您可以看到my functions中的函数。php文件

if ( !function_exists( \'wpex_pagination\' ) ) {

function wpex_pagination() {

$prev_arrow = is_rtl() ? \'→\' : \'←\';
$next_arrow = is_rtl() ? \'←\' : \'→\';

global $wp_query,$the_query;

if ( $the_query ) {
  $total = $the_query->max_num_pages;
  } else {
  $total = $wp_query->max_num_pages;
  }
  $big = 999999999; // need an unlikely integer
    if( $total > 1 )  {
         if( !$current_page = get_query_var(\'paged\') )
             $current_page = 1;
         if( get_option(\'permalink_structure\') ) {
             $format = \'page/%#%/\';
         } else {
             $format = \'&paged=%#%\';
         }
        echo paginate_links(array(
            \'base\'          => str_replace( $big, \'%#%\', esc_url( get_pagenum_link( $big ) ) ),
            \'format\'        => $format,
            \'current\'       => max( 1, get_query_var(\'paged\') ),
            \'total\'         => $total,
            \'mid_size\'      => 3,
            \'type\'          => \'list\',
            \'prev_text\'     => $prev_arrow,
            \'next_text\'     => $next_arrow,
      ) );
    }
  }
}
下面是我的模板页面上的循环页面新闻。php

<?php
        $the_query = new WP_Query( \'posts_per_page=5\' );
        if ($the_query -> have_posts()) :
        while ($the_query -> have_posts()) : $the_query -> the_post();
      ?>

      <div class="row blog-excerpts">
        <div class="col-md-4">
          <a href="<?php the_permalink(); ?>" class="angled-img">
            <div class="img">
              <?php the_post_thumbnail(); ?>
            </div>
          </a>
        </div>

        <div class="col-md-8">
          <div class="clearfix">
            <h3 class="h2 pull-left">
              <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
            </h3>
            <span class="date pull-right">
              <span class="glyphicon glyphicon-calendar"></span>
              <?php the_time( get_option( \'date_format\' ) ); ?>
            </span>
          </div>

          <div class="description">
            <?php the_excerpt(); ?>
          </div>

          <a href="<?php the_permalink(); ?>" class="btn">Read More</a>
        </div>
      </div>
      <?php endwhile; wp_reset_postdata(); wpex_pagination(); endif;  ?>
非常感谢您的帮助或指点。几天来,我一直在努力让这项工作发挥作用,并在互联网上阅读了许多文章。

1 个回复
最合适的回答,由SO网友:rémy 整理而成

您需要添加paged WPQuery的参数:

$paged = ( get_query_var(\'paged\') ) ? get_query_var(\'paged\') : 1;
$query = new WP_Query( array( \'posts_per_page\' => 5, \'paged\' => $paged ) );
这是docs.