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 list of posts from a category as seen here in this example. The problem is, if you scroll down to the bottom and click older, you'll see that the posts on the page have not changed at all, how ever the url has changed.

Can some one tell me whats wrong with the following code that generate this?

function category_query($query){
    if(is_admin || !$query->is_main_query()){
        return;
    }

    if(is_category()){
        $cat_id = get_cat_ID( single_cat_title(null, false) );
        $query->set("cat=$cat_id&posts_per_page=10" );
        return;
    }
}

add_action('pre_get_posts','category_query');

$attr = array(
    'align' => 'left',
    'class' => 'thumbnail imageRight',
    'width' => 350,
    'height' => 350
);
if(have_posts()){
    while(have_posts()): the_post();
    ?>
    <div class="post"><?php
        the_post_thumbnail('medium', $attr); ?>
        <h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
        <p><?php the_excerpt(); ?></p>
    </div>
        <?php
    endwhile;

    ?>
    <ul class="pager paddingBottom20">
      <li class="previous">
        <?php next_posts_link('&laquo; Older Entries'); ?>
      </li>
      <li class="next">
        <?php previous_posts_link('Newer Entries &raquo;'); ?>
      </li>
    </ul><?php 
}
share|improve this question

1 Answer 1

up vote 0 down vote accepted

Don't use query_posts in the template. If you delete that line, your template will paginate correctly.

If you want to alter the main query, use the pre_get_posts action.

Currently, your query overwrites the default main query, and you don't set any pagination parameters within the query, so you're always going to get the first 10 posts.

share|improve this answer

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.