Page navigation within a category

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.

<?php
 $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; ?>

Related posts

Leave a Reply

2 comments

  1. For this particular problem you need to change this..

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

    For..

    <?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.

  2. 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.