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 have a problem with the posts_nav_link() function on index.php. It works but it's not displaying the older posts. When someone clicks on the "next page" link the same posts from the fist page are loading.

This is the code I'm using:

<?php if ( is_home ()) {$myquery = new wp_query('cat=-41');}?> 
<?php
    global $wp_query;
    $temp_wp_query = $wp_query;
    $wp_query = null;
    $wp_query = $myquery;
?>      
    <!-- Start the look -->
    <?php if(have_posts()) : while(have_posts()) : the_post(); ?>
    <?php the_title();?>
        <?php
         if( has_post_thumbnail()) { 
            the_post_thumbnail('article-thumb'); }
         ?>
         <?php the_author_posts_link();?>
                     <?php the_category(', ');?>
                     <?php the_time('j F, Y'); ?>.
         <?php the_excerpt();?>
         <a href="<?php the_permalink();?>">Citeste mai departe</a>       
          <?php endwhile; ?>          
          <?php else : ?>
            <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>           
          <?php endif; ?><!-- End the loop -->
          <?php posts_nav_link( ' ', '<img src="' . get_bloginfo('stylesheet_directory') . '/img/pagina-precedenta.png" align="left" />', '<img src="' . get_bloginfo('stylesheet_directory') . '/img/pagina-urmatoare.png" align="right" />' ); ?>  
          <?php $wp_query = $temp_wp_query;?>

The posts_nav_link() function works properly on the category.php, only on index.php I have this problem and it's definitely related to my custom loop but I can't figure out what I'm missing.

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

You are missing the paged= paramter.

$myquery = new wp_query('cat=-41&paged=' . get_query_var( 'page' ));

Codex Note - You should set get_query_var( 'page' ) if you want your query to work with pagination.

Update -

Drop in this code in your theme's functions.php and remove all that your customization from top of loop. (7 lines)

function wpse61695_exclude_cat( $query )  {

    // making sure it runs on home page and on main query
    if ( $query->is_home() && $query->is_main_query() ) {

        $query->set( 'cat', '-41' );
    }
    return $query;
}
add_action( 'pre_get_posts', 'wpse61695_exclude_cat' );
share|improve this answer
    
Thanks for the answer amit. I've added the get_query_var( 'page' ) to the wp_query but it's still not working. I've also tried it without the cat=-41 but I got the same behavior. –  George Grigorita Aug 13 '12 at 16:44
    
using pre_get_post hook will not break the pagination. And you don't even need to modify the loop this way. –  amit Aug 13 '12 at 16:49
    
OK, I'll try to do it with pre_get_post - now reading about how to implement it. Thanks! –  George Grigorita Aug 13 '12 at 16:54
    
hm, I've updated answer with instruction and code, –  amit Aug 13 '12 at 17:09
    
Thanks, do I need to call the add_action( 'pre_get_posts', 'wpse61695_exclude_cat' ); somewhere in my index.php? I'm asking this because the posts from category ID 41 are still showing up on my homepage. –  George Grigorita Aug 13 '12 at 17:24
show 5 more comments

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.