Why isn’t my WordPress next_posts_link not working with exclude category loop?

I’m trying to setup a loop in WordPress that will show all posts from one category, which is working just fine, however my problem is that my ‘next_posts_link’ and ‘previous_posts_link’ doesn’t work. They navigate between pages just fine but the results are the same as the first page all the time.

<?php get_header(); ?>

<div id="main" role="main">

<?php
if (is_home()) {
query_posts("cat=-6");} //Exclude work posts (cat 6) from the news page
?>

<div class="inner">

<h1><?php trim(wp_title("")); ?></h1>

<?php include ('sidebartwo.php'); ?>

<section class="main-wrap twocol news">

<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>

<article class="box-style">

<time><?php the_time('M d') ?><span><?php the_time('Y') ?></span></time>

<h2><a href="<?php the_permalink()?>" title="<?php the_title(); ?>"><?php the_title(); ?>
</a></h2>

<?php the_content(''); ?>

</article>

<?php endwhile; ?>                              

<div class="next-prev-wrap">

<!-- This is what isn't working properly -->
<span class="next"><?php next_posts_link( 'Older posts', $post->max_num_pages ); ?></span>
<span class="prev"><?php previous_posts_link( 'Newer posts', $post->max_num_pages ); ?>
<!-- /end -->

</span>

</div>

</section>      

<?php endif; ?>

</div> <!-- /inner -->

</div> <!-- /main -->

<?php get_footer(); ?>

I don’t think I’m using the right syntax, in fact according to the WP codex page, I don’t even think my next/prev links are able to work the way I want it to. How should I approach this?

Related posts

Leave a Reply

1 comment

  1. Fixed myself. This post is now resolved. After much (and I mean much) Googling, I found this article which solved my problem: http://www.dynamicwp.net/articles-and-tutorials/pagination-problem-when-excluding-certain-category-from-blog-main-page/

    For reference my new code now looks like this:

    <?php get_header(); ?>
    
    <div id="main" role="main">
    
    <?php
    if (is_home()) {
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    query_posts("cat=-6&paged=$paged");
    }
    ?>
    
    <div class="inner">
    
    <h1><?php trim(wp_title("")); ?></h1>
    
    <?php include ('sidebartwo.php'); ?>
    
    <section class="main-wrap twocol news">
    
    <?php if (have_posts()) : ?>
    <?php while (have_posts()) : the_post(); ?>
    
    <article class="box-style">
    
    <time><?php the_time('M d') ?><span><?php the_time('Y') ?></span></time>
    
    <h2><a href="<?php the_permalink()?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>   </h2>
    
    <?php the_content(''); ?>
    
    </article>
    
    <?php endwhile; ?>                              
    
    <div class="next-prev-wrap">
    
    <span class="next"><?php next_posts_link( 'Older posts', $post->max_num_pages ); ?></span>
    <span class="prev"><?php previous_posts_link( 'Newer posts', $post->max_num_pages ); ?></span>
    
    </div>
    
    </section>      
    
    <?php endif; ?>
    
    </div> <!-- /inner -->
    
    </div> <!-- /main -->
    
    <?php get_footer(); ?>