RP McMurphy
RP McMurphy

Reputation: 744

Wordpress next/previous page links not working when at the second page

I lost track when actually the pagination stopped working. Cannot even find the problem. This is frustrating. Here is the code:

<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>

The first page next takes to the second page but the previous page link doesn't appear and at the same time, if I hit next on the second page, it stays on the second page. Have got enough posts to paginate.

Upvotes: 0

Views: 1846

Answers (1)

dkruchok
dkruchok

Reputation: 1909

Try to use "paged" parameter with custom WP_Query. Something like :

<?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; 
         ?>

Also you can try with "post_per_page" parameter

     <?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; 
     ?>

Or you can use plugin =) For example WP-PageNavi

Upvotes: 1

Related Questions