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 am having problems with pagination on category page that lists posts only within a certain category. Here is my code:

What happens is the Next link takes to /page/2 but the content stays the same.

<?php
    $id = get_cat_id($current_category);
    $my_query = new WP_Query('cat='. $id .'&showposts=2');
    while ($my_query->have_posts()) : $my_query->the_post();
?>
    <?php the_content( __( 'Continue reading <span class="meta-nav">&rarr;</span>', 'themename' ) ); ?> 
    <?php endif; ?>
<?php endwhile; ?>

<?php if (  $my_query->max_num_pages > 1 ) : ?>
    <nav id="nav-below">
        <div class="next"><?php previous_posts_link( __( '<span></span> Previous ', 'themename' ) ); ?></div>
        <div class="prev"><?php next_posts_link( __( 'Next <span></span>', 'themename' ) ); ?></div>
    </nav>
<?php endif; ?>
share|improve this question
    
eeyore: You need to set the 'paged' parameter yourself if you use a custom query. Is this your main query on the page? Is there a reason why you can't use and adapt the regular query? –  Jan Fabry Dec 29 '10 at 18:11
    
This seems very close to your previous question maybe it's best continued there for context? –  Rarst Dec 29 '10 at 19:44
    
@Jan - Yes, this is my main query on the page. Is there a better solution? –  eeyore Dec 30 '10 at 11:15
    
WordPress always executes one query while setting up the page. If you can it's better to modify this query, since then you only have to mind your specific changes and everything else (like paging) will be done by WordPress. The second part of Mike's answer does that. If you specify exactly what you don't like about the standard query, we can help you change it. –  Jan Fabry Jan 1 '11 at 11:17
add comment

3 Answers

Hi @eeyore:

Without being able to see the full context of your theme page I think you have at least two issues.

  1. You need to use $wp_query instead of $my_query if you want previous_posts_link() and next_posts_link() to work since they assume $wp_query.
  2. What @Jan Fabry mentioned; you need to capture the pagination yourself and pass it to WP_Query as a 'paged' parameter.
$url = $_SERVER['REQUEST_URI'];
if (strpos($url,'/page/')===false)
  $paged = 1;
else 
  $paged = preg_replace('#^.*/page/([0-9]+)/.*$#','\1',$url));
$wp_query = new WP_Query('cat='. $id ."&showposts=1&paged={$paged}");

That said, why are you creating your own query on this page? Is it so you can set the number of posts per page to be 2? If so, there's a much easier way and you can let WordPress handle pagination; add this to your theme's functions.php file:

add_action('pre_get_posts','yoursite_pre_get_posts2');
function yoursite_pre_get_posts2($query) {
  $query->set('posts_per_page',2);
}
share|improve this answer
1  
Why do you parse the URL to get the page parameter? Why not get it from $wp_query->get( 'paged' ) or $wp->query_vars['paged']? –  Jan Fabry Dec 29 '10 at 20:30
add comment

Is this the right way to do it? (I can't test this right):

<?php
    $id = get_cat_id($current_category);
    $wp_query = new WP_Query('cat='. $id .'&posts_per_page=3'.'&paged='.$paged);
    while ($wp_query->have_posts()) : $wp_query->the_post();
?>
    <?php the_content( __( 'Continue reading <span class="meta-nav">&rarr;</span>', 'themename' ) ); ?> 
    <?php endif; ?>
<?php endwhile; ?>

<?php if (  $wp_query->max_num_pages > 1 ) : ?>
    <nav id="nav-below">
        <div class="next"><?php previous_posts_link( __( '<span></span> Previous ', 'themename' ) ); ?></div>
        <div class="prev"><?php next_posts_link( __( 'Next <span></span>', 'themename' ) ); ?></div>
    </nav>
<?php endif; ?>
share|improve this answer
add comment

look at this article, this might solve your problem

Fix WordPress Pagination Problems when used with query_posts

share|improve this answer
add comment

Your Answer

 
discard

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