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?
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It only takes a minute to sign up.
Sign up to join this communityTo 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));
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.
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,,
$post_id
has to do with everything else?
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' );
Simplified version of the answer by Stefan Wimmer:
add_filter('get_the_terms', function ($terms, $post_id, $taxonomy) {
// from https://wordpress.stackexchange.com/a/230921
$exclude_categories = array(45);
if (!is_admin()) {
foreach($terms as $key => $term){
if($term->taxonomy == "category" && in_array($term->term_id, $exclude_categories)) {
unset($terms[$key]);
}
}
}
return $terms;
}, 100, 3);
I think that the call to get_term_by
can be omitted and used in_array
instead of a loop.