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've taken the example straight from the codex on this one, and my pagination is broken.

Here's the codex page I copied/pasted the code from: https://codex.wordpress.org/Function_Reference/next_posts_link

Everything is working. My posts are showing, and I get only the next_posts_link, however it only works until page 2. The previous_posts_link never appears.

I can only paginate to page 2, and I cannot go back or further.

I'm using WordPress 3.5.2 and I have reset my permalinks after attempting this.

I put a little debug section below to find how many max pages there are, the var_dump returns float(87).

And here's my PHP code. Any thoughts?

function homepage_test() {
    // Set the limit of posts to show per page
    $limit = 5;
    // set the "paged" parameter (use 'page' if the query is on a static front page)
    $paged = ( get_query_var( 'page' ) ) ? get_query_var( 'page' ) : 1;

    // the query
    $the_query = new WP_Query( 'showposts=' . $limit . '&paged=' . $paged . '&orderby=date&order=DESC' );

    if ( $the_query->have_posts() ) :
    // the loop
    while ( $the_query->have_posts() ) : $the_query->the_post(); 

        the_title();
    endwhile;

    // usage with max_num_pages
    next_posts_link( 'Older Entries', $the_query->max_num_pages );
    //Debug to show how many max pages.
    var_dump($the_query->max_num_pages);
    previous_posts_link( 'Newer Entries' );

    // clean up after our query
    wp_reset_postdata();
    else: ?>
        <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
    <?php endif;
}
share|improve this question
1  
Adjusting pagination in template is extremely unreliable. You need to be doing this in pre_get_posts hook or in othe way before template is reached. –  Rarst Jul 29 '13 at 14:07
    
And trying to paginate secondary queries with the ordinary *_posts_link functions is difficult if not impossible. Please take a look at the numerous "pagination" questions on the right under "Related". –  s_ha_dum Jul 29 '13 at 14:11
    
If it's difficult, why do they have it listed in the codex as an example (and even say, "set the "paged" parameter (use 'page' if the query is on a static front page)")? Most of the related posts deal with get_query_var( 'page' ), which I'm not having a problem with. –  Pat Jul 29 '13 at 14:43
    
@Rarst, I played with pre_get_posts, but it doesn't seem to be changing the outcome. Prev/Next links are still broken and/or only going to page 2. function homepage_posts( $query ) { if ( is_front_page() ) { $query->set( 'posts_per_page', 1 ); return; } } add_action( 'pre_get_posts', 'homepage_posts', 1 ); –  Pat Jul 29 '13 at 15:00
    
Also, I installed wp_pagenavi, and that's also not working. I'm at a complete loss with the code above and getting a page navigation working for paginated static front page. –  Pat Jul 29 '13 at 15:11
add comment

1 Answer

up vote 1 down vote accepted

previous_posts_link() echos the result of get_previous_posts_link() which checks the paged query variable, not the page query variable used on static pages. You will have to write your own code to display the link on a static page.

I didn't test it, but you might try fooling WordPress:

function homepage_test() {
    global $paged;

Now your changes to the $paged variable in your code will be seen by get_previous_posts_link(). I don't know what else it might affect or if the link will be correct.

share|improve this answer
    
As it looks right now, this seems to work great! (Won't know more until I get further into it, but at least I'm not stuck). Thanks!! –  Pat Jul 30 '13 at 12:45
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.