是的,您可以使用wp_get_object_terms
筛选以从使用默认WordPress方法的任何列表中删除隐藏类别,如wp_get_post_categories()
.
add_filter(\'wp_get_object_terms\', \'wpse_remove_unwanted_category\', 10, 4);
function wpse_remove_unwanted_category($terms, $object_ids, $taxonomies, $args) {
// we\'re only interested in category, if this is another taxonomy
if ($args[\'taxonomy\'] !== \'category\') {
// return early
return $terms;
}
// 34 and 1509 are ids of categories we want to remove
$remove_categories = array(34, 1509);
$filtered_terms = array_filter($terms, function($term) use ($remove_categories) {
// if term_id is in the array, filter it out (aka remove from array)
return ( ! in_array($term->term_id, $remove_categories));
});
return $filtered_terms;
}
但是,此过滤器将从许多列表中删除不需要的类别,而不仅仅是前端的类别。