Whenever you visit a URL in WordPress, WordPress automatically queries the correct posts for you based on that URL and your settings. For example if you visit example.com/blog
WordPress will query X number (defined in Settings > Reading) of your latest posts. Then if you visit example.com/blog/page/2
WordPress will query your latest posts offset by X, to get the second page. This is generally referred to as the “main” query.
To loop through posts in the main query, you use The Loop:
if ( have_posts() ) :
while ( have_posts() ) : the_post();
// Display post content
endwhile;
endif;
Notice how there’s no get_posts()
or WP_Query
. WordPress has already queried the correct post/s for that URL, so you just need to use the standard loop.
The issue with your template is that your pagination links are based on the main query, but the template is otherwise only displaying the results of get_posts()
. So if you go to /page/2
, WordPress will query the correct posts for page 2, but instead of displaying them your template is displaying the results of a completely separate query performed with get_posts()
, which is not changing its arguments based on the current page, so they’ll always be the same posts.
The solution is to remove any reference to the custom query and use the main loop:
<?php while ( have_posts() ) : the_post() ; ?>
<div class="blogrollentry">
<p class="blogrolltitle"><?php the_title(); ?></p>
<div class="container-fluid no-padding">
<?php if ( has_post_thumbnail() ) { the_post_thumbnail('post-thumbnail', array('class' => 'blogpostimage1')); } else { ?>
<img src="https://wordpress.stackexchange.com/questions/364628/<?php bloginfo("template_directory'); ?>/images/temp-h.jpg" class="blogpostimage1" alt="<?php the_title(); ?>" />
<?php } ?>
</div>
<p><?php echo intro_textblog(350); ?></p>
<a class="btn btn-block btn-outline-dark my-2 my-sm-0 biggertext" href="<?php echo get_permalink(); ?>">Read More</a>
</div>
<?php endwhile; ?>
<?php if ( $wp_query->max_num_pages > 1 ) : ?>
<div id="nav-below" class="navigation">
<div class="nav-previous"><?php next_posts_link( __( '<span class="meta-nav">←</span> Older posts', 'BTC' ) ); ?></div>
<div class="nav-next"><?php previous_posts_link( __( 'Newer posts <span class="meta-nav">→</span>', 'BTC' ) ); ?></div>
</div><!-- #nav-below -->
<?php endif; ?>