1 回答

TA貢獻(xiàn)1891條經(jīng)驗(yàn) 獲得超3個(gè)贊
您應(yīng)該能夠做到這一點(diǎn),而無(wú)需獲取排除項(xiàng)或雙循環(huán):
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;
}
如果您運(yùn)行的是 PHP 7.4+,您可以通過(guò)另一種方式編寫(xiě)此代碼以節(jié)省一些行:
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 關(guān)注
- 139 瀏覽
添加回答
舉報(bào)