0

Currently I'm facing a really strange problem I can't seem to fix normally. I've using pagination on my category pages. I've got a 'News' category with some sub categories. One sub category is 'MVO nieuws'.

When I visit '/mvo-nieuws' the pagination is working (showing an older posts link).

if ($paged) : ?>
    <nav class="page-nav archive">

        <?php if ($prev = get_previous_posts_link()) : ?>
            <div class="alignleft">
                <span class="fo icons-angle-circled-left"></span>
                <span><?php previous_posts_link( 'Nieuwe berichten' ); ?></span>
            </div>
        <?php endif;

        if ($next = next_posts($listPosts->max_num_pages, false)) : ?>
            <div class="alignright">
                <span>
                    <a href="<?= $next; ?>">Oudere berichten</a>
                </span>
                <span class="fo icons-angle-circled-right"></span>
            </div>
        <?php endif; ?>

    </nav>
<?php endif; ?>

The pagination is forwarding to '/mvo-nieuws/page/2', but that page couldn't be found. I'll found out this is because its a subcategory of 'News', but I don't know why this seems to be a problem.

I've removed the category as a subcategory and now it's working. All my other primary categories are also working fine, but the subcategories are facing this problem.

My permalink settings are: '/%category%/%postname%/'. Hopefully anyone can help me out, thanks in forward!

EDIT After Pieter Goosens his comment I thought it would be handy if you guys could see my query.

$cat = get_query_var('cat');
$category = get_category($cat);

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$listArgs = array(
    'cat' => $category->term_id,
    'paged' => $paged,
    'posts_per_page' => 11
);

$listPosts = new WP_Query($listArgs);

while ($listPosts->have_posts()) :
    $row++;
    $listPosts->the_post(); 
endwhile;

I'll hope this can help solve my problem.

My last code update: enter image description here

My pre_get_posts:

add_action( 'pre_get_posts', function ( $query ) {
    if (!is_admin() && $query->is_main_query() && $query->is_category())
        $query->set( 'posts_per_page', 11 );
});
  • Your issue is caused by a custom query which you should not be using in place of the main query. Simply revert back to the main default loop and your issue is sorted – Pieter Goosen Sep 24 '15 at 13:43
  • @PieterGoosen Thanks for your reply. Can you explain this to me? I'm using a custom WP_Query (see edited post). But I don't see how this should cause problems. – ronnyrr Sep 24 '15 at 16:22
  • Unfortunately I do not have the time to explain now, sorry about that, but you should read my answer here. If you still have issues, please feel free to leave a comment here and I will gladly have look tommorow :-) – Pieter Goosen Sep 24 '15 at 16:58
  • @PieterGoosen In your explanation you talk about using WP_Query. But I'm allready using the WP_Query as my main loop. Any idea? – ronnyrr Sep 24 '15 at 21:55
0

I think you misunderstood my linked post. It is a quite overwhelming amount of info (which you should reread as many times), but in short, you should not be using a custom query in place of your main query on the home page or any type of archive page.

I do not know why you are actually using a custom query, but if you need to alter the main query to adjust something, you should be using pre_get_posts or any of the posts_* filters where you can directly alter the generated SQL query.

You should first revert back to the default loop in your category page. You should remove all the code in your EDIT and replace it with

if ( have_posts() ) {
    while ( have_posts() ) {
    the_post();

        // YOUR HTML MARKUP AND TEMPLATE TAGS

    }
}

Your next posts link code does not make sense and also does not look correct. A note here, next_posts() is depreciated, you should use next_posts_link() and get_next_posts_link(). Look at the examples in the links on exact use case. Remember, the posts links are already setup to work with the main query by default, so you don't need any modifications.

Just one final note, you should have everything working at this point. Remember to do this on all your category pages if you are using different templates. If you need to adjust the amount of posts shown on a category page, you can use

add_action( 'pre_get_posts', function ( $q )
{
    if (    !is_admin() // Run only on front end queries
         && $q->is_main_query() // Only target the main query and not custom queries
         && $q->is_category() // Run only on category archive pages
    ) {
        // You can make use of all parameters in WP_Query
        $q->set( 'posts_per_page', 11 );

    }
});

If you need to remove posts from child categories on your category pages, you can make use of the parse_tax_query filter as described by @ialocin in his answer here

  • Thanks for you clear explanation about using the pre_get_posts. I wasn't aware of this hook and it's working. I've implemented the code in my website, but unfortunately it still isn't working. The next_posts_links still redirects to mvo-nieuws/page/2/ and than shows a blank page. You can find a live example here: www.mvodatabase.nl/mvo-nieuws. Maybe that can help figuring out this weird problem.. EDIT: in my main post I've uploaded my latest code via the image. – ronnyrr Sep 28 '15 at 20:22
  • I've found out that changing my permalink structure works. After changing /%category%/%postname%/ to /%category%/ I'll can use pagination, but I can't access single posts. Any idea why this might cause problems? – ronnyrr Sep 28 '15 at 20:42
  • Because I've thought this tread might get out of the first post' context I've created a new topic: wordpress.stackexchange.com/questions/203976/… – ronnyrr Sep 28 '15 at 20:55
  • You should accept my answer as my code solved your issue. As you have already stated, the issue with your permalinks should be a new question and does not have anything to do with this issue.;-) – Pieter Goosen Sep 29 '15 at 4:55
  • Allright then, I've marked your answer as solved. Although I think your answer didn't exactly solved my question. It made my code "more correct". But before I've changed my code my next_posts also redirected to mvo-nieuws/page/2/, so it did using your code. Thanks anyway for al the help! ;) – ronnyrr Sep 29 '15 at 18:19

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.