Hide categories from frontend, but not admin / post editor


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 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?

Ian Douglas 2 years 2020-12-17T07:10:48-05:00 0 Answers 8 views 0

Leave an answer