将代码段放入foreach循环中

时间:2015-10-05 10:18:03

标签: php wordpress foreach wordpress-plugin

我有一段代码,用于在向最终用户显示值之前,在名为“authors”的wordpress分类中用逗号代替双短划线。

这很有效,但现在我需要将它应用于几种不同的分类法,不仅仅是“作者”,还包括“打印机”,“翻译者”等。

我的想法是构建一个分类数组而不是单个变量,然后使用foreach循环将查找和替换应用于每个变量,但无论我如何尝试它,我似乎无法使其工作...

如果$ custom_taxonomy_type是一个数组,是否知道如何将以下代码转换为foreach循环?

这是目前适用于单一分类的代码。

if(!is_admin()){ // make sure the filters are only called in the frontend

$custom_taxonomy_type = 'authors';  // here goes your taxonomy type

function comma_taxonomy_filter($tag_arr){
    global $custom_taxonomy_type;
    $tag_arr_new = $tag_arr;
    if($tag_arr->taxonomy == $custom_taxonomy_type && strpos($tag_arr->name, '--')){
        $tag_arr_new->name = str_replace('--',', ',$tag_arr->name);
    }
    return $tag_arr_new;    
}
add_filter('get_authors', comma_taxonomy_filter);

function comma_taxonomies_filter($tags_arr){
    $tags_arr_new = array();
    foreach($tags_arr as $tag_arr){
        $tags_arr_new[] = comma_taxonomy_filter($tag_arr);
    }
    return $tags_arr_new;
}
add_filter('get_the_taxonomies',    comma_taxonomies_filter);
add_filter('get_terms',             comma_taxonomies_filter);
add_filter('get_the_terms',         comma_taxonomies_filter);

}

根据@Epodax的要求,这是我迄今为止尝试过的两件事。两者都会产生一个空白页面:

if(!is_admin()){ // make sure the filters are only called in the frontend

    $custom_taxonomy_type_array = array('authors', 'printer');  // here goes your taxonomy type

    foreach ($custom_taxonomy_type_array as $custom_taxonomy_type) {
        function comma_taxonomy_filter($tag_arr){
            global $custom_taxonomy_type;
            $tag_arr_new = $tag_arr;
            if($tag_arr->taxonomy == $custom_taxonomy_type && strpos($tag_arr->name, '--')){
                $tag_arr_new->name = str_replace('--',', ',$tag_arr->name);
            }
            return $tag_arr_new;    
        }
        add_filter('get_'.$custom_taxonomy_type, comma_taxonomy_filter);

        function comma_taxonomies_filter($tags_arr){
            $tags_arr_new = array();
            foreach($tags_arr as $tag_arr){
                $tags_arr_new[] = comma_taxonomy_filter($tag_arr);
            }
            return $tags_arr_new;
        }
        add_filter('get_the_taxonomies',    comma_taxonomies_filter);
        add_filter('get_terms',             comma_taxonomies_filter);
        add_filter('get_the_terms',         comma_taxonomies_filter);
    }


}

if(!is_admin()){ // make sure the filters are only called in the frontend

    $custom_taxonomy_type_array = array('authors', 'printer');  // here goes your taxonomy type

    function comma_taxonomy_filter($tag_arr){
        foreach ($custom_taxonomy_type_array as $custom_taxonomy_type) {
            global $custom_taxonomy_type;
            $tag_arr_new = $tag_arr;
            if($tag_arr->taxonomy == $custom_taxonomy_type && strpos($tag_arr->name, '--')){
                $tag_arr_new->name = str_replace('--',', ',$tag_arr->name);
            }
            return $tag_arr_new;    
        }
    }
    foreach ($custom_taxonomy_type_array as $custom_taxonomy_type) {
        add_filter('get_'.$custom_taxonomy_type, comma_taxonomy_filter);
    }

    function comma_taxonomies_filter($tags_arr){
        foreach ($custom_taxonomy_type_array as $custom_taxonomy_type) {
            $tags_arr_new = array();
            foreach($tags_arr as $tag_arr){
                $tags_arr_new[] = comma_taxonomy_filter($tag_arr);
            }
            return $tags_arr_new;
        }
    }
    foreach ($custom_taxonomy_type_array as $custom_taxonomy_type) {
        add_filter('get_the_taxonomies',    comma_taxonomies_filter);
        add_filter('get_terms',             comma_taxonomies_filter);
        add_filter('get_the_terms',         comma_taxonomies_filter);
    }

}

2 个答案:

答案 0 :(得分:1)

if(!is_admin()){//确保仅在前端调用过滤器

$custom_taxonomy_type = array ('form', 'package-type');

function comma_taxonomy_filter($tag_arr){
    global $custom_taxonomy_type;
    $tag_arr_new = $tag_arr;
    if($tag_arr->taxonomy == $custom_taxonomy_type[0] || $custom_taxonomy_type[1] && strpos($tag_arr->name, '--')){
        $tag_arr_new->name = str_replace('--',', ',$tag_arr->name);
    }
    return $tag_arr_new;    
}
add_filter('get_'.$custom_taxonomy_type, comma_taxonomy_filter);

function comma_taxonomies_filter($tags_arr){
    $tags_arr_new = array();
    foreach($tags_arr as $tag_arr){
        $tags_arr_new[] = comma_taxonomy_filter($tag_arr);
    }
    return $tags_arr_new;
}
add_filter('get_the_taxonomies',    comma_taxonomies_filter);
add_filter('get_terms',             comma_taxonomies_filter);
add_filter('get_the_terms',         comma_taxonomies_filter);

我的代码是有效的 " ||"

答案 1 :(得分:0)

如果没有你的设置,它会有点难,但我认为使用in_array而不是需要for循环,这样的事情应该有效(未经测试,只显示已更改的函数) :

$custom_taxonomy_types = array('authors', 'printers', 'translators');

function comma_taxonomy_filter($tag_arr){
    global $custom_taxonomy_types;
    $tag_arr_new = $tag_arr;
    if(in_array($tag_arr->taxonomy, $custom_taxonomy_types) && strpos($tag_arr->name, '--')){
        $tag_arr_new->name = str_replace('--',', ',$tag_arr->name);
    }
    return $tag_arr_new;    
}

修改

看起来你也需要这样的东西(基于你的新代码);我错过了添加过滤器:

foreach ($custom_taxonomy_types as $custom_taxonomy_type) {
    add_filter('get_'.$custom_taxonomy_type, comma_taxonomy_filter);
}