Hide a certain category name without removing it?

I have a category called feature which I use to have a feature post in the homepage. The problem is I don’t want the category name feature to appear in this posts, but I want the other categories to show up.

How could I do that?

5 Answers
5

To achieve this we need to use get_the_category here.
I am going to use this code in several places, so it is more efficient to create a function

function exclude_cats($excludedcats = array()){

    $categories = get_the_category();
    $separator=", ";
    $output="";
    foreach($categories as $category) {
        if ( !in_array($category->cat_ID, $excludedcats) ) {
            $output .= 'term_id ).'" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $category->name ) ) . '">'.$category->cat_name.''.$separator;
        }
    }
    echo trim($output, $separator);

}

All that I’ve done inside the function was to call get_the_category function, But I’ve excluded the categories I don’t want to show their names to not show.

inside the index I’ve called the function like so exclude_cats(array(11, 40, 53));

Leave a Comment