可以在应用逻辑修改类别之前添加条件语句。例如,我添加了一个检查,如果我们不在类别存档页面上,它将退出并返回未修改的类别列表:
// 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 );
}