Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.


share|improve this question
    
next_posts_link acts on the main (global) $wp_query object, and you have a custom query displaying posts... it won't work –  adrian7 Sep 5 '13 at 20:14
    
Hmm... I've tried changing it to $wp_query->new WP_Query ($args) and still no luck. What else do you recommend? I have also changed to query_posts() which didn't make any difference. –  Eric Sep 5 '13 at 20:31
add comment

1 Answer 1

up vote 1 down vote accepted

Try the following:

Reference: posts_nav_link

<div class="navigation">
<div class="alignleft"><?php previous_posts_link('&laquo; Previous Entries') ?></div>
<div class="alignright"><?php next_posts_link('Next Entries &raquo;','') ?></div>
</div>

Don't use next_post_link or previous_post_link they are used for single post pagination.

share|improve this answer
    
Tried that, and they don't display any nav on the page at all. –  Eric Sep 4 '13 at 22:19
    
can you try to echo $paged and tell us what it echos –  Rahil Wazir Sep 4 '13 at 22:31
    
Sure. $paged == 1 both before and after The Loop. –  Eric Sep 4 '13 at 22:44
    
My mistake: you're right. I was passing too many parameters to previous_posts_link() and it was getting confused. (See in my original question, where I pass 3 parameters? Turns out it only accepts one. Dur.) Thanks. –  Eric Sep 5 '13 at 20:48
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.