如何才能改善这种粗略的功能呢?

时间:2011-01-25 作者:Scott B

在下面的第一个函数中,在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 );
    }

3 个回复
最合适的回答,由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;
}
SO网友:Cronco

我唯一能看到的是,你打电话给get_cat_ID 针对每个学期的每个类别。这是每个函数调用的db查询。

foreach 并将它们放入作为参数发送给函数的数组中。

SO网友:tw2113

也许可以尝试将要检查的术语添加到数组中,然后只检查数组中是否有$cat\\u id。我想说有一些类似的函数,但我不能随便把它们放在一起。这样就不需要手术室了

结束

相关推荐

WordPress有没有内置的引用它运行的PHP版本?

我有一个插件,它有一个需要PHP5的方法。我如何围绕服务器可能具有PHP4这一事实进行编码?代码如下。。。(我认为是DOMDocument对象在PHP4服务器上运行时冻结了插件。function rseo_doTheParse($heading, $post){ $content = $post->post_content; if($content==\"\" || is_php4()) return false; $keyword = trim(st