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 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; ?>
share|improve this question
    
The codex sais nothing of the max_num_pages property for the $wp_query (codex.wordpress.org/Function_Reference/WP_Query). Are you sure it exists? edit - found it in the source code. my bad. –  Cronco Dec 22 '10 at 1:47

2 Answers 2

up vote 1 down vote accepted

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.

share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.