vigoX
vigoX

Reputation: 11

How to hide a specific category from every blog post in Wordpress

I have a category with a name "FP" (for featured post) That I use to insert my most important blogs into a slider. The problem is that "FP" is being shown under the title of every blog post in the slider (which is logical and normal) I would like to hide "FP" and just show the rest. Is that possible?

Thanks!

Upvotes: 0

Views: 1216

Answers (1)

vigoX
vigoX

Reputation: 11

I found the solution, this code should be added to functions.php (the code includes comments to show exactly how it works):

function filter_get_the_category( $categories, $post_id ) {
  // loop post categories
  foreach ($categories as $index => $category) {
    // check for certain category, you could also check for $term->slug or $term->term_id
    if ( 'Featured Posts' === $category->name ) {
      // remove it from the array
      unset( $categories[$index] );
    }
  }
  return $categories;
}
add_filter( 'get_the_categories', 'filter_get_the_category', 10, 2 );

Solution by Antti Koskinen

Update: For newbies like me: If you have more than one category that you would like to hide you can use the || (or) operator. The line after editing looks like this:

if ( 'Featured Posts' === $category->name || 'Main Featured Post' === $category->name || 'FeaturedPosts1' === $category->name || 'WhereTo' === $category->name ) {

Upvotes: 1

Related Questions