at WordPress blog Post Pagination Showing Same Posts Every Page
My loop
<?php
$args = array( 'numberposts' => 12, 'order'=> 'DESC', 'orderby' => 'date' );
$postslist = get_posts( $args );
foreach ($postslist as $post) : setup_postdata($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="<?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 endforeach; ?>
<?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; ?> ``
at WordPress blog Post Pagination Showing Same Posts Every Page
My loop
<?php
$args = array( 'numberposts' => 12, 'order'=> 'DESC', 'orderby' => 'date' );
$postslist = get_posts( $args );
foreach ($postslist as $post) : setup_postdata($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="<?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 endforeach; ?>
<?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; ?> ``
Share
Improve this question
edited Apr 23, 2020 at 6:27
fuxia♦
107k39 gold badges255 silver badges459 bronze badges
asked Apr 20, 2020 at 20:41
Ahmed TawfekAhmed Tawfek
131 bronze badge
2
|
1 Answer
Reset to default 2Whenever 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/blog
WordPress will query X number (defined in Settings > Reading) of your latest posts. Then if you visit example/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="<?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; ?>
get_posts()
and not using the main query? – WebElaine Commented Apr 20, 2020 at 21:48