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 created my category.php to display all the posts (i am using custom post) in the category.

By default, it's displaying 10 entries, and when i scroll down, i wanna fetch the next 10 entries with jquery infinite scroll. The problem is when i added the following lines to my template, it shows the link(href) to "/category/category_name/page/2" which returns 404.

<?php if($loop->max_num_pages > 1) : ?>
<nav id="page-nav" class="navigation" role="navigation">
    <?php previous_posts_link('≪ Previous', $loop->max_num_pages); ?>
    <?php next_posts_link('More ≫', $loop->max_num_pages); ?>
</nav>
<?php endif; ?>

How should i set my link (href) so that it fetch the next 10 entries in the same category?

Thank you.

share|improve this question
    
Please edit your question to include your custom query parameters. –  Chip Bennett May 24 '13 at 15:02

1 Answer 1

up vote 1 down vote accepted

category.php is the main category archive template file, which is auto-magically loaded by WordPress for category archives. In that case, use the pre_get_posts hook to alter the main query for this page and you shouldn't have trouble with the pagination.

function pregp_wpse_100602($qry) {
  if (is_category() && is_main_query()) {
    // no idea what conditions you want, but below is a sample
    // $qry->set('posts_per_page',5);
  }
}
add_action('pre_get_posts','pregp_wpse_100602');

As is, you've created a second loop-- $loop->-- for the page. The pagination functions do not work well when you do that. Those functions depend on the main query, which you are not using. I believe that is the root of the problem.

Related

Wordpress pagination on custom script
http://wordpress.stackexchange.com/a/77545/21376

share|improve this answer
    
+1. I'd give more up-votes if I could. –  Chip Bennett May 24 '13 at 14:47
    
Also: I think the OP would want something like query->set( 'post_type', array( 'post', 'cpt-slug' ) );, where 'cpt-slug' is the slug passed to register_post_type(). This will add the CPT to the category query. –  Chip Bennett May 24 '13 at 14:49
    
@ChipBennett : Yes, I am guessing something like that as well but I am not really sure. That does seem to be the point of this, though the wisdom of hijacking the entire category archive is questionable. –  s_ha_dum May 24 '13 at 14:52
1  
The OP needs to post the current custom query parameters, so we can understand what is currently being done (the wisdom of that action may or may not be relevant :) ). –  Chip Bennett May 24 '13 at 14:54
    
thanks for the answers. I realized that there is nothing wrong with the href as showned on the question, the problem is i am using custom post type, which shows 404 when it fetch the link custom_post_type/page/2 , and i found this plugin which solved my problem! Custom Post Type Category Pagination Fix –  rei May 24 '13 at 17:15

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.