0

I have set up a custom loop that is working fine, but will not display the pagination links. After some searching around I've learned that the pagination links (as I've used them here anyway, but I'm not aware of an alternative) only support the original loop, but all of the "solutions" I'm running across are telling me that if I explicitly declare the $paged parameter in the wp_query, it should work, and if it doesn't that I should just use query_posts instead. That seems like a wishy-washy answer and I would like to know how to make it work with WP_QUERY, so here is the code I've cobbled together from various sources:

<?php
global $paged;
$paged = (get_query_var('page')) ? get_query_var('page') : 1;
$blogquery = new WP_Query('&post_type=post&post_status=publish&posts_per_page=1&paged='.$paged);
  if ( $blogquery->have_posts() ) { echo '<h2 class="h-bord">'; _e('Posts:','abdtrans'); echo '</h2>';
    while ( $blogquery->have_posts() ) { $blogquery->the_post(); ?>
        <div class="post-single">
            <!---- [POST CONTENT] ---->
        </div>
<?php } } ?>

<div class="oldernewer">
<p class="older"><?php next_posts_link(__('&laquo; Older Entries', 'abdtrans')) ?></p>
<p class="newer"><?php previous_posts_link(__('Newer Entries &raquo;', 'abdtrans')) ?></p>
</div>

<?php wp_reset_query(); ?>

Any ideas? Thanks for the help!

0

You are most probably not doing this on a static front page, so your paged parameter is using wrong values. Static front pages uses get_query_var( 'page' ) to calculate pagination, all other pages uses get_query_var( 'paged' )

You need to also set the $max_pages parameter in next_posts_link. Without that, your links won`t display

<p class="older"><?php next_posts_link(__('&laquo; Older Entries', 'abdtrans')) ?></p>

should be

<p class="older"><?php next_posts_link(__('&laquo; Older Entries', 'abdtrans'), $blogquery->max_num_pages) ?></p>

Just a final note, you should never ever revert to query_posts. It breaks more stuff than it "fixes"

  • Thanks Pieter, worked perfectly! I did try 'paged' first and it didn't work before, but I was unaware of the max_pages requirement in the links. – Shoelaced Mar 9 '15 at 18:16
  • My pleasure, glad it worked out for you. Enjoy :-) – Pieter Goosen Mar 9 '15 at 18:16

Your Answer

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

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