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.

The loop below works fine, apart from the fact that the pagination will always stop on page 6. No matter what arguments i specify, it will never show more than 6 pages.

Does anyone know why?

<?php 
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;            
    $idxargs = array(
        'posts_per_page' => '1',
        'paged' => $paged,
    );

    $idxloop = new WP_Query( $idxargs );                   
?>

    <?php if ($idxloop->have_posts()) : while ( $idxloop->have_posts() ) : $idxloop->the_post(); ?>                  
        <?php the_title(); ?>                    
    <?php endwhile; ?>

   <?php next_posts_link( __( 'Next') ); ?>
   <?php previous_posts_link( __( 'Prev' ) ); ?>

    <?php endif; ?>
    <?php wp_reset_query(); ?>
share|improve this question

2 Answers 2

up vote 0 down vote accepted

Trying to use pagination from a different query is always fraught with potential disaster. next_posts_link accepts a second argument, $max_pages, which may solve your issue if you pass it properly:

next_posts_link( __( 'Next'), $idxloop->max_num_pages );

However, the real answer to this question is to adjust your query before the template. Adjusting the default query via a new WP_Query or by using query_posts is quite simply doing it wrong, despite the millions of examples on the web you will see that do exactly that.

The preferred method for this is using a pre_get_posts action along with a conditional check to apply it to the specific type of query you want to adjust:

function wpa64918_homepage_posts_per_page( $query ) {
    if ( $query->is_home() && $query->is_main_query() )
        $query->set( 'posts_per_page', 1 );
}
add_action( 'pre_get_posts', 'wpa64918_homepage_posts_per_page', 1 );
share|improve this answer
    
Thanks for the answer, but how does that function know what the "is_main_query" is on the page? I have about 3 loops on the homepage (aside from the main 1). –  Blackbird Sep 12 '12 at 20:12
    
the main query is the query that happens before the template is loaded, based on the requested page. WP sets a variable in the query object to identify it as such. see the action reference page to see the general process of how a request is parsed, posts are loaded, then the template is loaded. this is why it's inefficient to call query_posts or create a new query to replace the original, because you're just overwriting or not using a query that's already happened before you ever reached the template. –  Milo Sep 12 '12 at 21:04
    
The function seems to trigger the extra pages in the pagination, but most of the higher numbers are 404 errors. I've cleared my permalinks too... thx for help –  Blackbird Sep 15 '12 at 8:37
    
it's difficult to say without seeing your full template. you've got an issue with your loops polluting the global variables or being used incorrectly somewhere. –  Milo Sep 15 '12 at 14:33

How many posts per page have you specified inside your Settings -> Reading screen?

Also, try placing a wp_reset_query(); at the top of your script, too.

UPDATE:

Try this loop:

<?php query_posts('posts_per_page=1&paged='.$paged.''); ?>
<?php if(have_posts()) : while(have_posts()) : the_post(); ?>
<li>
    <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
</li>
<?php endwhile; ?>
<?php next_posts_link( __( 'Next') ); ?>
<?php previous_posts_link( __( 'Prev' ) ); ?>
<?php endif; ?>
share|improve this answer
    
Changing posts per page in settings seems to trigger something. It was set at 5. Strangely the more i up this setting, the less pages show in pagination (20 only shows 2 pages). I set it to "2" and now i have 16 pages (thought some of these are empty). Seems this is on the right track, thx. –  Blackbird Sep 12 '12 at 8:57
    
Also wp_reset_query(); at top doesn't help. I even deleted all loops before the problem loop. –  Blackbird Sep 12 '12 at 8:58

Your Answer

 
discard

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

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