So I have created dedicated page template, and on this page I want to list the 3 most recent blog posts, with the usual pagination links to take users to the previous or next 3 posts.
I have the list populated, and the links are appearing, but when I click on either the previous or next links I just get the same 3 posts as before. I can see the URL changes (/blog/page/2) but the posts being shown are always the three most recent ones.
UPDATE. After getting very frustrated I decided to take things back to basics. The following snippet is the only code I had in my template, just to isolate the loop basically, and this STILL didn’t fix it. All I get is a singple post on the page, BUT if I manually type /page/2 at the end of the url it takes me to page 2 with a different post showing. However, the only link I see to do with pagination is ‘Newer Posts’ (and that only appears on page 2). How come ‘older’ posts isn’t showing up?
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 1,
'paged' => $paged,
);
$postslist = get_posts( $args );
foreach ($postslist as $post) : setup_postdata($post);
?>
<article>
<h2 class="fnt25 noBtmMarg"><a href="<?php echo get_permalink(); ?>" title="Read: <?php the_title(); ?>"><?php the_title(); ?></a></h2>
<div class="meta fnt18 pMarginBottom">Posted on: <?php the_date(); ?></div>
<div class="fnt22"><?php print_custom_field('listingDesc'); ?></div>
<a href="<?php echo get_permalink(); ?>" class="floatRight fnt22" title="Read the full article: <?php the_title(); ?>">Read more...</a>
<div class="clearfix"></div>
</article>
<?php endforeach; ?>
<div class="nav-previous alignleft"><?php next_posts_link( 'Older posts' ); ?></div>
<div class="nav-next alignright"><?php previous_posts_link( 'Newer posts' ); ?></div>
Okay. After doing a little bit of research myself, it seems that the
query_posts()
is not the best for this type of scenario. Use get_posts(), read up on it a bit as well.A good example (EDIT: better description):
Would you please check above code?