• Resolved Corneliatt

    (@corneliatt)


    Hi!
    I have a totally annoying problem that I can’t find a solution on. I want to use:
    <?php next_post_link('%link', '&raquo;', '', '5'); ?>
    to exclude a category from the single blog post navigation. The problem is that I can’t get the exclusion to work.
    Could it be because the blog posts connected to the category ID 5 also are connected to other categories? Or why isn’t it working? Would be very grateful if someone could please help me out here πŸ™‚

Viewing 8 replies - 1 through 8 (of 8 total)
  • If some posts have 2 or more categories, they will probably still show up. Not sure tbh. Try a testpost.

    Thread Starter Corneliatt

    (@corneliatt)

    Ok, so now I know for certain that it’s because the blog posts are connected to other categories as well. Is there a way to tell next-post-link to “don’t show the blog posts connected to this category, even though it’s also in another category”??

    What you could do, is to exclude that category from your main query using the category__not_in parameter. That might do it. Not sure tbh.
    http://codex.ww.wp.xz.cn/Class_Reference/WP_Query#Category_Parameters

    Thread Starter Corneliatt

    (@corneliatt)

    Hi!
    After some searching I found this script:
    (functions.php)

    function prev_next_dont_include( $ids ) {
    foreach ( get_categories() as $category ) : // loop thru all WP cats
        if ( !in_array( $category->cat_ID, $ids ) ) : // check if cat id is in $ids
            $categories[] = $category->cat_ID; // build list of real excluded ids
        endif;
    endforeach;
    return implode( ' and ', $categories ); // "1 and 2 and 5 and 7 and 16 and 23"
    }

    (loop-single.php)
    <?php next_post_link('%link', '&raquo;', false, prev_next_dont_include(array(5))); ?>

    This script seem to do the opposite of what I want. When using this, the navigation is just between blog posts in category ‘5’..

    Is there a way to edit this to make it exclude all categories except 5?

    Would be so grateful if someone could please help me!

    I found the same script. Try removing the ! on rule 3. So instead of !in_array you enter in_array.

    But I don’t think this code will work. It assumes you can set an include_category parameter inside the next_post_link() which you can’t.

    Let me know.

    Thread Starter Corneliatt

    (@corneliatt)

    No, doesn’t work :/ It would had been too easy I guess :S
    Have been searching the web for a looong time now without success. It’s kind of weird that it doesn’t seem to be any solutions on this problem out there. There must be a whole bunch of people wanting to do the same as me!

    So, back to your suggestion on category_not_in.. Where do I change the main query for single pages? I just seem to find queries for category pages and so on…

    Use pre_get_posts http://codex.ww.wp.xz.cn/Plugin_API/Action_Reference/pre_get_posts of WP_Query to alter the loop.

    Doing this will probably give you the same problem but it will also give you a lot of options to solve that problem.

    The main point being, next_post_link() should work because the posts from certain categories won’t be returned by the loop.

    Keep us informed on your progress.

    Thread Starter Corneliatt

    (@corneliatt)

    Ok, so I found a plugin that solves this without any hacking. It’s called Ambrosite Next/Previous Post Link Plus and using this I just added:
    <?php previous_post_link_plus( array('order_by' => 'post_date', 'format' => '%link', 'link' => '<span class="paddy">&raquo;</span>', 'in_same_cat' => true, 'ex_cats' => '5', 'ex_cats_method' => 'strong') ); ?>

    and problem solved!! πŸ™‚ Thanks for your help!

Viewing 8 replies - 1 through 8 (of 8 total)

The topic ‘Exclude certain category in next_post_link’ is closed to new replies.