WordPress.org

Ready to get started?Download WordPress

Forums

Exclude subcategories from the_category (4 posts)

  1. trex33
    Member
    Posted 5 years ago #

    Someone posted this brilliant solution to exclude categories from the_category (placed in functions.php of theme).

    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);

    Questions:
    1. How can I change to this exclude by category ID instead of by category name?
    2. Is there way to automatically exclude subcategories/children of the excluded categories? (i.e., without explicity listing all of the subcats)

  2. trex33
    Member
    Posted 5 years ago #

    FYI, I found a great plugin related to issue of the_category. Instead of excluding categories from the_category, it enables you to show the subcatories of a specific category that your post is associated with. Use it in the loop.

    http://oneserving.com/site-news/wordpress-plugin-show-post-subcategories/

  3. testcricket
    Member
    Posted 5 years ago #

    Re your first question - I used the tags as explained in here

    Template Tags wp-list-categories

    There is also this plugin

    advanced-category-excluder

  4. Malice
    Member
    Posted 5 years ago #

    ACE doesn't allow to exclude subcategories :(

Topic Closed

This topic has been closed to new replies.

About this Topic