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); ?>
<?php endif; ?>

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

Thank you.

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

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);

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.


Wordpress pagination on custom script

  • 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

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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