Exclude a category from the filed under list only on some templates

You can add conditional statements before applying the logic to modify the categories. For example, I added a check that will bail and return the unmodified list of categories if we’re not on the category archive page:

// Add the filter to 'the_category' tag.
add_filter( 'the_category', 'the_category_filter', 10, 2 );
function the_category_filter( $thelist, $separator=" " ) {

    // Bail if this is not the category archive page.
    // https://developer.wordpress.org/reference/functions/is_tax/
    if ( ! is_tax( 'category' ) ) {
        return $thelist;
    }

    // list the IDs of the categories to exclude
    $exclude = array(4,5);
    // 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 );
}

Leave a Comment