I have a template that displays 3 blog posts per page. I want to use Wordpress pagination for previous/next, but the pagination won't display on my page.
Here's the code I have now for template-blog.php
(UPDATE: I moved this to home.php
as well):
if ( get_query_var('paged') ) { $paged = get_query_var('paged'); }
elseif ( get_query_var('page') ) { $paged = get_query_var('page'); }
else { $paged = 1; }
$args = array('post_type' => 'post', 'paged' => $paged);
$the_query = new WP_Query( $args );
while ( $the_query->have_posts() ) {
$the_query->the_post();
// (I display the post here)
}
<div class='pagination'>
<?php previous_posts_link('%link', '<span>Previous</span>', TRUE); ?>
<?php next_posts_link('%link', '<span>Next</span>', TRUE); ?>
</div>
This code displays posts #1 through #3 but no next/previous link. There are still two more posts (#4 and #5) which should be on the next page of posts, so there should be a "Previous" link. And if I manually change the URL to /blog/2
, sure enough, there are posts #2 and #1.
(I also tried previous_post_link
and next_post_link
, for per-post navigation, and they display a link that goes to just the previous post. Which is no good.)
UPDATE 1: I noticed that $the_query->max_num_pages == 2
, which is correct (I've loaded 5 test posts into the system). So at least Wordpress knows there's another page.
UPDATE 2: I changed my code to use query_posts()
... still no luck, previous_posts_link
and next_posts_link
displays nothing.
UPDATE 3: I've moved this code into home.php
, and changed Settings > Reading to make this page the main "posts" page. Now I can confirm that Wordpress is using home.php
as the template file to display the blog page, but it still won't display any pagination.
next_posts_link
acts on the main (global)$wp_query
object, and you have a custom query displaying posts... it won't work$wp_query->new WP_Query ($args)
and still no luck. What else do you recommend? I have also changed toquery_posts()
which didn't make any difference.