Support » Themes and Templates » Excluding categories in the_category

  • In my index.php template, I of course want to show the categories associated respectively with each post. So I use the default code:

    <?php the_category() ?>

    I would like to have ‘the_category’ only hook into 5 categories (under which all the posts are) and ignore the rest of the categories (which I use for other aspects of the blog). In other words, I’d like to include only a few specific categories.

    ‘wp_list_categories’ has the ability to do this, but that lists ALL categories, not just the ones associated with a post. Example:

    <?php
    wp_list_categories('orderby=name&include=3,5,9,16'); ?>

    Is what I’m looking for possible?

    Thank you!

Viewing 13 replies - 16 through 28 (of 28 total)
  • hi nlex, thank you for trying to help me.
    But that also didnt work…

    Because:
    mCatFilter for WordPress allows you to exclude categories from The Loop for display on the home page, in feeds and in archive pages.

    Only remove from home page, feeds and archive pages…
    I only to remove from posts 🙂

    Anyone else, please?

    i’m not sure to understand, personally i just want not to display a specific category in my sidebar and in my posts. In fact i would like this category to exist but not to be displayed. I found a way for my sidebar but not for posts and plugins doesn’t works.

    I found this article but i don’t know if it can help :
    http://zeo.unic.net.my/notes/exclude-category-in-wordpress/

    I have been looking for a solution for something like this and I make some modifications to some code posted in various topics and I get this

    <?php foreach((get_the_category()) as $cat) {
    if (!($cat->cat_ID==’#’)) echo $cat->cat_name . ‘ ‘;
    } ?>

    #= yout cat ID.

    I hope it works for you.

    <?php foreach((get_the_category()) as $category) {
    if ($category->cat_ID <> ’15’) { echo apply_filters(‘the_category’, $category->cat_name) . ‘ / ‘; }}
    ?></div></div>
    <?php wp_link_pages(); ?>

    yes i’m using similar code and it worked. The only different thing i that i filter the result because i’m using Language Switcher extension wich is a multilingual plugin… maybe it will help someone at some point…

    thanks anyway

    hi

    Needed to do this as well so improving on chavo’s and nlex’s code above (many thanks) this below will do what you want and give you exactly the same type of output as the_category() i.e. categories with links on them.

    //exclude these from displaying
    $exclude = array("Large Announcement", "Announcement", "News");
    
    //set up an empty categorystring
    $catagorystring = '';
    
    //loop through the categories for this post
    foreach((get_the_category()) as $category)
    {
    	//if not in the exclude array
    	if (!in_array($category->cat_name, $exclude))
    	{
    		//add category with link to categorystring
    		$catagorystring .= '<a href="'.get_bloginfo(url).get_option('category_base').'/'.$category->slug.'">'.$category->name.'</a>, ';
    	}
    }
    
    //strip off last comma (and space) and display
    echo substr($catagorystring, 0, strrpos($catagorystring, ','));

    I have coded it as an exclude list of categories (i.e. don’t show categories in the list) as I have fewer to exclude but you might want to do it exactly the way you said initially, i.e. only show the categories in your list, an include list. if so change:

    $exclude = array(“Large Announcement”, “Announcement”, “News”);
    to
    $include = array(“Large Announcement”, “Announcement”, “News”);

    and insert as many categories as you want to display (inside the brackets in double quotes and seperated by commas) and then change:

    if (!in_array($category->cat_name, $exclude))
    to
    if (in_array($category->cat_name, $include))

    a+
    gar

    I was just working on the same solution

    <?php foreach((get_the_category()) as $cat) {
    if (!($cat->cat_ID=='37')) echo '<a href="' . get_bloginfo('url') . '/category/' . $cat->category_nicename . '/">'. $cat->cat_name . '</a>' . ', ';
    } ?>

    That’s a nice clean solution asquare. Thanks!

    Otto42: I feel that this feature for the_category() is needed. I’m in the process of creating a site that uses categories for certain topics but the post also indexes itself within a glossary of terms/common phrases/etc. It is merely an aesthetic aspect for this site and I will admit that having each post list a single letter as a category looks confusing and strange.

    I ran into a similar problem. I wanted to have a featured posts list. The complicating factor was that I was using the related posts plugin so I could not use tags without doing a code adjustment in the plugin (as far as I could tell). Also excluding categories would have caused me to have to do all kinds of (ambiguous) code rewriting throughout the template files.

    I came across this codex page:
    http://codex.wordpress.org/Template_Tags/query_posts

    I choose to use the ‘meta_key’ custom field parameter. As this would allow me to keep tags and categories separated from the featured content, and allow for the choosing of featured posts in the backend.

    I added the following code inside the loop of the index.php:

    if (is_home()) {
         query_posts('meta_key=featured');
      }

    Then I created a key in the custom fields section of the post in the backend named ‘featured’ with the value ‘featured’. Once a key was created in one post I could select the key in other or new posts through a selection menu in the custom fields sections. I do have to type the value into the value field everytime I want to add the key otherwise it won’t take. Not sure why this is, or how to fix it so it does it automagically. For end-users it would save a step and get rid of the risk of randomly added end-user values. There is probably a function that could do this, but unfortunately I haven’t had a change to look into it.

    Besides that it works like an charm and all posts are selected on basis of the meta_key.

    For those of you who might be lazier and just want a drop-in function that filters the_category() call across your theme pages, you can put this in functions.php:

    function the_category_filter($thelist,$separator=' ') {
    	if(!defined('WP_ADMIN')) {
    		//list the category names to exclude
    		$exclude = array('Something','Something Else','Blah','YAY');
    		$cats = explode($separator,$thelist);
    		$newlist = "";
    		foreach($cats as $cat) {
    			$catname = trim(strip_tags($cat));
    			if(!in_array($catname,$exclude))
    				$newlist .= $cat.$separator;
    		}
    		$newlist = rtrim($newlist,$separator);
    		return $newlist;
    	} else
    		return $thelist;
    }
    add_filter('the_category','the_category_filter',10,2);

    mtaunk, strip the trailing comma on the last category and you’re gold.

    superann… now we know where you got the name. Doing this across themes is the way to go.

    Moderator Samuel Wood (Otto)

    (@otto42)

    WordPress.org Admin

    the_category_filter($thelist,$separator=' ') {
    	if(!defined('WP_ADMIN')) {
    		//list the category names to exclude
    		$exclude = array('Something','Something Else','Blah','YAY');
    		$cats = explode($separator,$thelist);
    		$newlist = array();
    		foreach($cats as $cat) {
    			$catname = trim(strip_tags($cat));
    			if(!in_array($catname,$exclude))
    				$newlist[] = $cat;
    		}
    		return implode($separator,$newlist);
    	} else
    		return $thelist;
    }
    add_filter('the_category','the_category_filter',10,2);

    No ending comma.

    Awesome! I love WordPress!!!

    @otto42 Thank you! Don’t forget ‘function’ at the beginning. This is the code for everyone else:

    function the_category_filter($thelist,$separator=' ') {
    	if(!defined('WP_ADMIN')) {
    		//list the category names to exclude
    		$exclude = array('Something','Something Else','Blah','YAY');
    		$cats = explode($separator,$thelist);
    		$newlist = array();
    		foreach($cats as $cat) {
    			$catname = trim(strip_tags($cat));
    			if(!in_array($catname,$exclude))
    				$newlist[] = $cat;
    		}
    		return implode($separator,$newlist);
    	} else
    		return $thelist;
    }
    add_filter('the_category','the_category_filter',10,2);

    This function should be included in every theme!

    I’m using the function on http://www.freesoftwareworkshop.com

Viewing 13 replies - 16 through 28 (of 28 total)
  • The topic ‘Excluding categories in the_category’ is closed to new replies.