0

I'm using the following code, placed in the functions.php of my theme, to hide certain categories used to organise posts and populate sliders from the posts themselves and their category listings:

function the_category_filter($thelist,$separator=' ') {
 // list the IDs of the categories to exclude
 $exclude = array(1,32,42,4);
 // create an empty array
 $exclude2 = array();

 // loop through the excluded IDs and get their actual names
 foreach($exclude as $c) {
      // store the names in the second array
      $exclude2[] = get_cat_name($c);
 }

 // get the list of categories for the current post     
 $cats = explode($separator,$thelist);
 // create another empty array      
 $newlist = array();

 foreach($cats as $cat) {
      // remove the tags from each category
      $catname = trim(strip_tags($cat));

      // check against the excluded categories
      if(!in_array($catname,$exclude2))

      // if not in that list, add to the new array
      $newlist[] = $cat;
 }
 // return the new, shortened list
 return implode($separator,$newlist);
}

// add the filter to 'the_category' tag
add_filter('the_category','the_category_filter', 1, 32, 42, 4);

This works well, but it also hides the categories in the post edit backend. I can see the tick box, but the name of the category is hidden.

Can someone suggest a tweak to the above code that preserves its function of hiding categories on the frontend, but keeps categories visible in the post edit window / the WP backend?

2 Answers 2

1

Just don't apply the filter if is_admin().

Unrelated- you should also take a look at add_filter, your arguments are not correct.

2
0

You can do something like:

if ( ! is_admin() ) {

// Exclude categories.

}

More examples in: https://developer.wordpress.org/reference/functions/is_admin/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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