Exclude posts from WordPress Navigation in Thematic

Let’s say you have a certain category that you want to be displayed some place other than on your main blog page. No big deal, you just exclude your special category from the main index loop.

Now, you’re probably also want to make some changes to the single post navigation, and make it so that single posts in your special category won’t get posts from any other category in their navigation links. And also, chances are you’ll want to exclude posts from your special category from the navigation links of posts in all other categories.

In other words, in the navigation links of single posts from your special category you’ll only get posts from that category, and in the navigation links of posts from all other categories, you’ll get posts from all categories except your special category. Two separate navigations.

For all of this, all you need to do is use the following two Thematic filters: thematic_previous_post_link_args and thematic_next_post_link_args. If you’re interested to look into the code, you can find the filters being applied in thematic/library/extensions/content-extensions.php

And here’s the code snippet to include in your child-theme’s functions.php:

function custom_previous_post_link_args($args){
	if(in_category(3)){
		$args['in_same_cat'] = TRUE;
	}
	else{
		$args['excluded_categories'] = 3;
	}
	return $args;
}
add_filter('thematic_previous_post_link_args', 'custom_previous_post_link_args');
 
function custom_next_post_link_args($args){
	if(in_category(3)){
		$args['in_same_cat'] = TRUE;
	}
	else{
		$args['excluded_categories'] = 3;
	}
	return $args;
}
add_filter('thematic_next_post_link_args', 'custom_next_post_link_args');

About Cristian Antohe

Cristian Antohe is the co-founder Cozmoslabs. He's a WordPress developer, web designer and Open Source fanatic. He's part of the team that developed Profile Builder and WordPress Creation Kit and also helps curate wpMail.me.

Find me on: Google + | Twitter

10 thoughts on “Exclude posts from WordPress Navigation in Thematic

  1. Thanks! Exactly what I was looking for…almost. How do I exclude more than one category? (I’m a designer and not so much a coder.)

    • Hey, Melanie!

      Try and replace 3 with “3,5,6”

      Don’t forget the quotation marks.

      You also need to replace

      in_category(3)

      with

      in_category(3) || in_category(5) || in_category(6)

      • Thanks for you reply.

        I made the changes you suggested (and I remembered the quotation marks), but it seems to only workfor the first category I listed.

        Here’s my code (in case you see something I don’t!):

        function custom_previous_post_link_args($args){
        if(in_category(103) || in_category(107)){
        $args['in_same_cat'] = TRUE;
        }
        else{
        $args['excluded_categories'] = "103,107";
        }
        return $args;
        }
        add_filter('thematic_previous_post_link_args', 'custom_previous_post_link_args');

        function custom_next_post_link_args($args){
        if(in_category(103) || in_category(107)){
        $args['in_same_cat'] = TRUE;
        }
        else{
        $args['excluded_categories'] = "103,107";
        }
        return $args;
        }
        add_filter('thematic_next_post_link_args', 'custom_next_post_link_args');

    • Damn! I had to dive deep into the murky waters of WordPress for this one.

      I got to the bottom of this by taking a look at the get_adjacent_post() function in wp-includes/link-template.php

      Apparently, a simple comma won’t do. The 'excluded_categories' argument needs to be a string with the following format: "103 and 107"

      I’ve even tested it and it works. Just replace "103,107" with "103 and 107" and everything should be fine.

  2. Since you were so helpful with Melanie, maybe you can find a plugin that lets you exclude posts. I am also a newbe, so any help that is simple is appreciated.

  3. Hi – thank you so much for this post, exactly what I needed.

    Question: I have used a snippet from here: http://blogmum.com/2009/04/how-to-exclude-categories-from-the-home-page-of-your-wordpress-blog/ in a Thematic child theme to exclude a category (Downloads) from the home page, and used the above code to keep the post nav within that category. However, I also want to keep the home page nav within only home page ‘news’ posts, but currently it still links to ‘Downloads’ posts, and I can’t work out how to modify the code above to do that.

    Any suggestions or pointers much appreciated. See the issue here (dev site) http://www.jaijiel.net/hiddenorchestra

    Thanks

  4. Doh!

    Ok, Melanie’s comment (with Ganriel’s correction) is what I needed. The question arose because there is a chance that I could end up with 2 Categories being on the home page – News and Uncategorized – if the client forgets to specify. How would the code need to be modified in that case?

    Apologies for the stupidity, I’m more of a CSS coder, still struggling with PHP learning curve.

Leave a Reply

Your email address will not be published. Required fields are marked *


five − = 1

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>