1

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?

1

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

0

How about using get_the_terms filter like this,,

add_filter('get_the_terms', 'hide_categories_terms', 10, 3);
function hide_categories_terms($terms, $post_id, $taxonomy){

    // get term to exclude by name, by id or maybe by slug
    $exclude = get_term_by('name', 'feature', 'category', ARRAY_A);

    if (!is_admin()) {
        foreach($terms as $key => $term){
            if($term->taxonomy == "category"){
                if(in_array($key, $exclude)) unset($terms[$key]);
            }
        }
    }

    return $terms;
}

i think it should works,,

  • I don't really understand how this could work. What the $post_id has to do with everything else? – TCB13 May 8 '16 at 10:56
0

I think @Lafif Astahdziq was on the right track - I like his approach even though it is fairly static. Here is how I rewrote it:

add_filter('get_the_terms', 'hide_categories_terms', 10, 3);
function hide_categories_terms($terms, $post_id, $taxonomy){

    // define which category IDs you want to hide
    $excludeIDs = array(6);

    // get all the terms 
    $exclude = array();
    foreach ($excludeIDs as $id) {
        $exclude[] = get_term_by('id', $id, 'category');
    }

    // filter the categories
    if (!is_admin()) {
        foreach($terms as $key => $term){
            if($term->taxonomy == "category"){
                foreach ($exclude as $exKey => $exTerm) {
                    if($term->term_id == $exTerm->term_id) unset($terms[$key]);
                }
            }
        }
    }

    return $terms;
}

Put this in your functions.php and it will work.

0

An old question but still quite popular so I have added another solution to add to your theme's functions.php file.

function hide_feature($query){
    if($query->is_main_query() && $query->is_home()){
        //negate the ID of the 'feature' category e.g. '-6'
        $query->set('cat', '-6');   
    }
}
add_action( 'pre_get_posts', 'hide_feature' );
  • As of WordPress 5.01, I've tried this, and it does not appear to work. – omega33 Feb 22 at 6:13
  • Hi, I've had a look through the code for this on WordPress github and nothing has changed recently but only certain fields (those with IDs) can be negated this way. Also there can be no space between the dash and the number. The code specifically for categories can be found here: github.com/WordPress/WordPress/blob/… – Andy Gee Feb 23 at 7:38
  • Thanks Andy. I'll have a look into that. Very much wanting to get this working. – omega33 Feb 23 at 8:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.