Wordpress下一页/上一页链接在第二页时无法正常工作

时间:2016-06-08 17:50:03

标签: wordpress pagination

当实际分页停止工作时,我失去了轨道。甚至找不到问题。这令人沮丧。这是代码:

<section class="content-section">

    <div class="col-md-10 col-md-offset-1">
        <?php
        if( have_posts() ):
        while( have_posts() ): the_post();
        get_template_part( 'template-parts/content', get_post_format() );
        endwhile;

        ?>
    </div>

    <div class="col-md-10 col-md-offset-1 text-center">
        <div class="blog-pagination">
            <?php
                next_posts_link('<span class="pagination-btn btn-center">Back</span>');
                previous_posts_link('<span class="pagination-btn">Next</span>');
            ?>
        </div>
    </div>
    <?php endif; ?>

</section>

第一页接下来第二页,但上一页链接没有出现,同时,如果我在第二页上点击下一页,则它会保留在第二页上。有足够的帖子分页。

1 个答案:

答案 0 :(得分:1)

尝试在自定义WP_Query中使用“paged”参数。 类似的东西:

<?php
       if (have_posts()) :
        $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
        $args = array(
            'paged' => $paged
            );
        $loop = new WP_Query($args);
        while ($loop->have_posts()) : $loop->the_post();
        get_template_part( 'template-parts/content', get_post_format() );
        endwhile;
         ?>

    <div class="col-md-10 col-md-offset-1 text-center">
        <div class="blog-pagination">
            <?php
           next_posts_link('<span class="pagination-btn btn-center">Back</span>');
           previous_posts_link('<span class="pagination-btn">Next</span>'); 
          ?>
        </div>
    </div>
     <?php 
       wp_reset_postdata();
      endif; 
         ?>

您也可以尝试使用“post_per_page”参数

     <?php
       if (have_posts()) :
        $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
        $args = array(
            'paged' => $paged
            'posts_per_page' => '10'

            );
        $loop = new WP_Query($args);
        while ($loop->have_posts()) : $loop->the_post();
        get_template_part( 'template-parts/content', get_post_format() );
        endwhile;
         ?>

   <div class="col-md-10 col-md-offset-1 text-center">
      <div class="blog-pagination">
        <?php
       next_posts_link('<span class="pagination-btn btn-center">Back</span>', $loop->max_num_pages);
       previous_posts_link('<span class="pagination-btn">Next</span>', $loop->max_num_pages); 
      ?>
     </div>
  </div>
  <?php 
   wp_reset_postdata();
  endif; 
     ?>

或者你可以使用plugin =) 例如WP-PageNavi