Home » Php » Pagination in wordpress is not working

Pagination in wordpress is not working

Posted by: admin October 26, 2017 Leave a comment


I have created a custom made home page where there are name of the categpries. When I click on the categories, I am redirecting to another page named as ” page-category.php “, passing the category ID in URl. In page-category.php page I want to capture the id and show the customized post design with photos and excerpt of that particular category. However I want pagination on my page in case number of posts exceed a certain number. None of the pagination function is working properly. here is my code:

$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
 $npsPosts = new WP_Query('posts_per_page=5&cat='.$cat_id.'&paged=' . $paged);
            if($npsPosts -> have_posts()):

        next_posts_link( 'Older Entries', $the_query->max_num_pages );
        previous_posts_link( 'Newer Entries' );

                while($npsPosts->have_posts()): $npsPosts ->the_post();
                    if ( !in_array( $post->ID, $do_not_duplicate )) {  ?>
                        <div class="horizontal_post_grid_row_col cat-page-box">
                            <div class="horizontal_post_grid_row_col-img">
                                <a href='<?php the_permalink();?>'><?php the_post_thumbnail('medium-thumbnail'); ?></a>
                            <div class="horizontal_post_grid_row_col_content">
                                <h3 style="margin-bottom:5px;"><a href='<?php the_permalink();?>'><?php the_title();?></a></h3>
                                <span class="author_tag">Reading time : <?php echo reading_time(); ?></span> 
                                <div class="excerpt_content"><?php the_excerpt(); ?></div>
                                <div class='keep_reading_text'><a href='<?php the_permalink();?>'>Read more</a></div>
                        <div class="gap-cat">jkn</div>


                      next_posts_link( 'Older Entries', $the_query->max_num_pages );
          previous_posts_link( 'Newer Entries' );

I haven’t added anything in function.php.When I click the category option the I am sending the URL like “wwww.example.com?id=108” . I am catching the ID in page-category.php and displaying all the posts of that category ID. The URL becomes “www.example.com/category/?id=108”. When using pagination it becomes like “www.example.com/category/page/2/?id=108”. If it is not the right approach please suggest.


remove functions next_posts_link,previous_posts_link
and add below code:

$big = 999999999; // need an unlikely integer

echo paginate_links( array(
    'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
    'format' => '?paged=%#%',
    'current' => max( 1, get_query_var('paged') ),
    'total' => $npsPosts->max_num_pages
) );