在下面的第一个函数中,在foreach内部,我调用了第二个函数,以测试是否存在要从类别列表中删除的匹配类别ID。
然而,在我看来,我在第二个函数中的操作方式相当粗糙,因为没有更好的术语。如何改进此查找?
function admin_trim_category_description( $terms, $taxonomies )
{
if( \'category\' != $taxonomies[0] )return $terms;
foreach( $terms as $key=>$term )
{
$terms[$key]->description = strip_tags(substr( $term->description, 0, 75 ))."...";
if(ce4_get_utility_cats($terms[$key]->term_id))
{
unset($terms[$key]);
}
}
return $terms;
}
function ce4_get_utility_cats($cat_id)
{
if($cat_id == get_cat_ID(\'category1\') OR $cat_id == get_cat_ID(\'hidden\') OR $cat_id == get_cat_ID(\'category2\') OR $cat_id == get_cat_ID(\'category3\'))
{
return true;
}
else
{
return false;
}
}
编辑:以上函数按以下方式调用。。。
add_action( \'admin_head-edit-tags.php\', \'admin_edit_tags\' );
function admin_edit_tags(){
add_filter( \'get_terms\', \'admin_trim_category_description\', 10, 2 );
}
最合适的回答,由SO网友:onetrickpony 整理而成
function admin_trim_category_description( $terms, $taxonomies ){
if( \'category\' != $taxonomies[0] )return $terms;
$whatever_categories = array(\'category1\', \'hidden\', \'category2\', \'category3\');
foreach( $terms as $key => $term)
if(!in_array($terms[$key]->name, $whatever_categories)) $terms[$key]->description = strip_tags(substr( $term->description, 0, 75 ))."...";
else unset($terms[$key]);
return $terms;
}