0

I have a custom page template on my site, with a WP_Query pulling in a list of posts. I'd like to implement pagination but just can't seem to get it right. Here's the code I'm using blelow, which pulls in the most recent 2 posts fine, but then doesn't display the 'next' or 'previous' links as you might expect.

For clarity, I have a total of 5 posts that meet the criteria in the query and this template is being used as the front page of my blog. Also, the inc-post-listing.php simply pulls in certain info from each post, like the thumbnail, title and permalink.

I'm sure I'm missing something obvious so any help is very much appreciated.

 <?php 
        $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
        // WP_Query arguments
        $args = array (
            'post_type'              => 'post',
            'post_status'            => 'publish',
            'posts_per_page'         => '2',
            'paged'                  => $paged
        );

        // The Query
        $posts = new WP_Query( $args );

        // The Loop
        if ( $posts->have_posts() ) {
            while ( $posts->have_posts() ) {
                $posts->the_post(); 
                include('inc-post-listing.php');
                }
        } else {
            // no posts found
        }

        // Restore original Post Data
        wp_reset_postdata();
    ?>



    <div><?php posts_nav_link(' ','Previous Page','Next Page'); ?></div>
    <div><?php posts_nav_link(' ','Go Back','Show More Tips'); ?></div>
  • 2
    There is no 'pagination' parameter for WP_Query. Please, see the Codex for the correct pagination parameters. Algo, this question and its answers may be helpful for you. – cybmeta May 19 '14 at 11:27
  • Ah ok, that was provided b a WP_Query generator script, I should let them know it doesn't exist. Thanks for the link I have tried updating my code (added back into my OP), but it's still not working. The next/previous links just don't show up... Am I getting closer? – Kev May 19 '14 at 12:44
  • The next previous links are outside the query, so there's no logical reason they should have anything to do with it – Tom J Nowell May 19 '14 at 12:45
  • Also is the whole point of this query just to change the number of posts shown to 2? – Tom J Nowell May 19 '14 at 12:46
  • That makes sense - I've tried moving them to sit just before the reset_postdata, but that didn't seem to help... Nah I'm using the query on a page template that would otherwise only display the content of the specific page. – Kev May 19 '14 at 12:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.