Reputation: 415
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>
Upvotes: 1
Views: 1955
Reputation: 4870
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1; // setup pagination
$the_query = new WP_Query( array(
'post_type' => 'post',
'paged' => $paged,
'post_status' => 'publish',
'orderby' => 'date',
'order' => 'DESC',
'posts_per_page' => 1)
);
while ( $the_query->have_posts() ) : $the_query->the_post();
echo '<div>' . get_the_title() . '</div>';
the_content();
endwhile;
echo '<nav>';
echo '<div class="nav-previous alignleft">'.get_next_posts_link('Older posts', $the_query->max_num_pages).'</div>'; //Older Link using max_num_pages
echo '<div class="nav-next alignright">'.get_previous_posts_link('Newer posts', $the_query->max_num_pages).'</div>'; //Newer Link using max_num_pages
echo "</nav>";
wp_reset_postdata(); // Rest Data
Would you please check above code?
Upvotes: 0
Reputation: 1787
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):
<?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' => 3,
'paged' => $paged,
);
$postslist = get_posts( $args );
foreach ($postslist as $post) : setup_postdata($post);
?>
Upvotes: 1