1 回答

TA贡献1891条经验 获得超3个赞
您应该能够做到这一点,而无需获取排除项或双循环:
add_filter('get_the_terms', 'hide_categories_terms', 10, 3);
function hide_categories_terms($terms, $post_id, $taxonomy){
if ( ! is_admin() && is_single() ) {
// filter for terms that are not in the exclude array
$filtered_terms = array_filter($terms, function($term) {
$excludeIDs = array(1, 322, 320, 321);
return ! in_array($term->term_id, $excludeIDs);
});
// return filtered array of terms
return $filtered_terms;
}
// return default terms JIC the above case is not met
return $terms;
}
如果您运行的是 PHP 7.4+,您可以通过另一种方式编写此代码以节省一些行:
add_filter('get_the_terms', 'hide_categories_terms', 10, 3);
function hide_categories_terms($terms, $post_id, $taxonomy){
if ( ! is_admin() && is_single() ) {
$excludeIDs = [1, 322, 320, 321];
// filter for terms that are not in the exclude array
$filtered_terms = array_filter($terms, fn($t) => ! in_array($t->term_id, $excludeIDs));
// return filtered array of terms
return $filtered_terms;
}
// return default terms JIC the above case is not met
return $terms;
}
- 1 回答
- 0 关注
- 117 浏览
添加回答
举报