WordPress.org

Ready to get started?Download WordPress

Forums

Excluding categories in the_category (29 posts)

  1. crimsonemperor
    Member
    Posted 6 years ago #

    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!

  2. moshu
    Member
    Posted 6 years ago #

    http://codex.wordpress.org/Template_Tags/the_category
    Does it have exclude parameter? No, it doesn't.

  3. crimsonemperor
    Member
    Posted 6 years ago #

    Right....it does not have an exclude or an include parameter... I'm not sure how that helped.

    How can I exclude/include categories, then?

  4. moshu
    Member
    Posted 6 years ago #

    You can't.

  5. crimsonemperor
    Member
    Posted 6 years ago #

    OK, thank you :)

  6. MichaelH
    Member
    Posted 6 years ago #

    How about using Tags for one of those functions, and Categories for the other?

  7. cezarneaga
    Member
    Posted 6 years ago #

    hi,

    i want the same thing too. since i see there is no include/exclude parameter shouldn't it be possible to do a little bit or workaround?

    i mean the_category reads and displays all categories a post has but isnt there a way to tell the loop to echo only certain ones?

    i am not a programmer but couldnt it be something like:

    <?php
    $category = get_the_category();
    echo $category[9,10,11]->cat_ID;
    ?>

    where 9,10,11 are the category ids of the categories you want to display. i tried it and it didnt work but i think i just dont know syntax :)

  8. nlex
    Member
    Posted 5 years ago #

    up

  9. testept
    Member
    Posted 5 years ago #

    up... please someone help cezarneaga :)

  10. Samuel Wood (Otto)
    Tech Ninja
    Posted 5 years ago #

    This really makes no sense. If you don't want a category to display on a post, then don't assign the post to that category in the first place.

    If you're using categories for something else, then you're probably using them wrong and making things overcomplicated. Find something else to use instead of categories for your custom stuff.

  11. testept
    Member
    Posted 5 years ago #

    I´m not using them wrong and not making things overcomplicated...
    Only have a movies blog, and I want to separate them by genre.
    The problem is that appears like that in my blog:

    Genre: Action, Horror, Upcomings, Featured

    And I need to remove "Upcomings" and "Featured" categories from the the_category list...

    Any good idea? :)
    Until now, this was the best idea that I found:
    http://designintellection.com/2007/excluding-wordpress-categories-with-css/

    But I don't want to use, because don't work in all versions of browsers...

  12. shaneholden
    Member
    Posted 5 years ago #

    Is this what you are looking for? http://wordpress.org/extend/plugins/simply-exclude/

  13. testept
    Member
    Posted 5 years ago #

    Sorry mate, that plugin dont work for me...
    But thank you for trying help :)

  14. Samuel Wood (Otto)
    Tech Ninja
    Posted 5 years ago #

    A custom filter on the_category would let you remove them, but you'd need to know some PHP and such.

    function my_category_filter($catlist) {
    // $catlist contains the list of links, add code to edit it here
    return $catlist;
    }
    add_filter('the_category','my_category_filter');
  15. testept
    Member
    Posted 5 years ago #

    Thank you Otto42 :)

    Damn, my PHP knowledge is zero lol... bad luck
    Can someone with a good soul and heart, help me? :D

    Thank you

  16. nlex
    Member
    Posted 5 years ago #

    i tried this plugin but it doesn't work for me ?

    maybe it will work better for you ?

    http://www.xhaleera.com/index.php/products/wordpress-mseries-plugins/mcatfilter/

  17. testept
    Member
    Posted 5 years ago #

    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?

  18. nlex
    Member
    Posted 5 years ago #

    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/

  19. chavo
    Member
    Posted 5 years ago #

    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.

  20. nlex
    Member
    Posted 5 years ago #

    <?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

  21. asquare
    Member
    Posted 5 years ago #

    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

  22. Anonymous
    Unregistered
    Posted 5 years ago #

    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>' . ', ';
    } ?>
  23. mocha
    Member
    Posted 5 years ago #

    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.

  24. design_dolphin
    Member
    Posted 5 years ago #

    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.

  25. superann
    Member
    Posted 5 years ago #

    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);
  26. stevesidea
    Member
    Posted 5 years ago #

    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.

  27. Samuel Wood (Otto)
    Tech Ninja
    Posted 5 years ago #

    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.

  28. Tammy Hart
    Member
    Posted 5 years ago #

    Awesome! I love WordPress!!!

  29. nicholas77
    Member
    Posted 5 years ago #

    @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

Topic Closed

This topic has been closed to new replies.

About this Topic