I would like to know why my page navigation within a particular category doesn't work.

The code seems correct but I never see the pagination links.

 $my_query = new WP_Query('cat=18&showposts=8');
 while ($my_query->have_posts()) : $my_query->the_post();
 <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>

  <?php the_content(); ?>

 </article><!-- #post-<?php the_ID(); ?> -->

<?php endwhile; ?>

<?php if (  $wp_query->max_num_pages > 1 ) : ?>
 <nav id="nav-below">
  <h1 class="section-heading"><?php _e( 'Post navigation', 'themename' ); ?></h1>
  <div class="nav-previous"><?php next_posts_link( __( '<span class="meta-nav">&larr;</span> Older posts', 'themename' ) ); ?></div>
  <div class="nav-next"><?php previous_posts_link( __( 'Newer posts <span class="meta-nav">&rarr;</span>', 'themename' ) ); ?></div>
 </nav><!-- #nav-below -->
<?php endif; ?>

2 Answers 2


For this particular problem you need to change this..

<?php if (  $wp_query->max_num_pages > 1 ) : ?>


<?php if (  $my_query->max_num_pages > 1 ) : ?>

However, like Rarst said, if you're looking to change the "main" query, then query_posts is really what you're looking for, and the change above wouldn't be required under those conditions(simply update your query to call query_posts and not WP_Query). It's hard to tell what you're doing just from what's posted though, so if you're unsure, just make the above change.


Where are you trying to do this? Which context (page, archive, etc) / template?

Posts links functions pull info from global variables. Code you posted uses its own query object, which doesn't affect global variables.

If you are doing this in archive's page template and want to change main Loop then you should use query_posts() function instead of query object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.