WordPress custom page template pagination gives more pages than what exists

enter image description here

I have a custom page template page-homepage.php with a custom loop and paginate_links() function.

The same thing is set up on another custom page template page-video.php and it works as expected.

However, on page-homepage.php the pagination block shows me 10 pages even though I only have 1 other post to show, so the pagination should only show 2 pages instead of 10 and when I click on “Next” the URI changes to /page/2 but the same post is displayed on the second page and third page…

<?php
$paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
// WP_Query arguments
$args = array(
    'post_type'              => array( 'post' ),
    'post_status'            => array( 'publish' ),
    'nopaging'               => false,
    'offset'                 => 8,
    'posts_per_page'         => 1,
    'paged'                  => $paged
);

// The Query
$latest = new WP_Query( $args );
$max_num_pages = $latest->max_num_pages;
// The Loop
if ( $latest->have_posts() ) {
while ( $latest->have_posts() ) {
$latest->the_post(); ?>
<div class="well">
  <div class="media">
    <a class="pull-left" href="#">
      <?php the_post_thumbnail('latest-posts-thumbs', array('class' => 'media-object img-responsive')); ?>
    </a>
    <div class="media-body">
      <h2 class="media-heading post-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
      <?php the_excerpt(); ?>
      <div class="post-details">
        <div class="col-md-6 pull-left text-left no-padding">
          <span class="post-author"><?php the_author_posts_link(); ?></span> <span class="post-date"><?php echo get_the_date(); ?></span>
        </div>
        <div class="col-md-6 pull-right text-right no-padding">
          <a href="<?php comments_link(); ?>"><?php comments_number('0','1','%' );?> <i class="fa fa-comment-o"></i></a>
        </div>
      </div>
    </div>
  </div>
</div>
<?php }

$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' => $latest->max_num_pages
) );

} else {
// no posts found
}
// Restore original Post Data
wp_reset_postdata();
?>

This is a screenshot of what the pagination looks like, it really should show only 2 pages instead of 10.

Read more here: WordPress custom page template pagination gives more pages than what exists

Leave a Reply

Your email address will not be published. Required fields are marked *