WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] filtering the_category (9 posts)

  1. poisonborz
    Member
    Posted 5 years ago #

    Howdy,
    How could I filter the_category? I want to hide specific categories from the list (eg. 'uncategorized')... like how the 'exclude' parameter works in wp_list_categories. I suspect it could be done with get_the_category, but don't know how...

  2. MichaelH
    Member
    Posted 5 years ago #

    I've use this before:

    <?php
    foreach((get_the_category()) as $category) {
        if ($category->cat_name != 'cat_to_exclude') {
        echo '<a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> ';
    }
    }
    ?>
  3. NathanSpike
    Member
    Posted 5 years ago #

    MichaelH , thx a lot for you help , I see 2 times this code but ... where I put the hide category ? I want to exclude the category with the ID number 204 ..

    I try many test but nothin :/

    Can you tell me where i write the ID , to exclude the category ? :o

    thx alot

  4. bigsquaredot
    Member
    Posted 5 years ago #

    @NathanSpike: Replace cat_to_exclude with the actual category ID that you want to exclude and the code should work.

  5. Steve Bruner
    Member
    Posted 5 years ago #

    How can you filter out more than one category?

  6. achellios
    Member
    Posted 5 years ago #

    @NathanSpike: actually, you'll have to edit the code slightly to exclude categories by ID. Code is as follows:

    <?php
    foreach((get_the_category()) as $category) {
        if ($category->cat_ID != '204') {
        echo '<a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> ';
    }
    }
    ?>

    You need to change "cat_name" to "cat_ID" in order for it to exclude by ID #. I also changed cat_to_exclude to your ID # for ya. Hope that helps!

  7. achellios
    Member
    Posted 5 years ago #

    @sbruner: This probably isn't the best way to do it, but the following works:

    <?php
    foreach((get_the_category()) as $category) {
        if ($category->cat_name != 'category1' &amp;&amp; $category->cat_name != 'category2') {
        echo '<a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> ';
    }
    }
    ?>

    Be sure to replace &amp;&amp; with && when you implement your code (WP forums won't let me use ampersand inside code tags).

  8. cebradesign
    Member
    Posted 5 years ago #

    I was looking for some kind of solution like that. Thanks!

  9. andreacianni
    Member
    Posted 4 years ago #

    I find this solution here, I think it's work well, and look like very clean!

    code to add in the function.php in your theme folder:

    function the_category_filter($thelist,$separator=' ') {
    	if(!defined('WP_ADMIN')) {
    		//Category IDs to exclude
    		$exclude = array(1,5);
    
    		$exclude2 = array();
    		foreach($exclude as $c) {
    			$exclude2[] = get_cat_name($c);
    		}
    
    		$cats = explode($separator,$thelist);
    		$newlist = array();
    		foreach($cats as $cat) {
    			$catname = trim(strip_tags($cat));
    			if(!in_array($catname,$exclude2))
    				$newlist[] = $cat;
    		}
    		return implode($separator,$newlist);
    	} else {
    		return $thelist;
    	}
    }
    add_filter('the_category','the_category_filter', 10, 2);

    if you want to filter more than 2 category, you have to change the last number in the last line of the cove above

    take a look here for the add_filter reference

Topic Closed

This topic has been closed to new replies.

About this Topic