2015-10-05 102 views
0

在向最終用戶顯示值之前,我有一段代碼用於替換名爲「authors」的wordpress分類中的逗號的雙破折號。使代碼片段進入foreach循環

這是偉大的工作,但現在我需要把它應用到幾個不同的分類,不只是「作者」,而且還「打印機」,「翻譯」等

我的想法是建立一個數組分類法而不是單個變量,然後使用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); 
    } 

} 

回答

0

這一點很難,而無需您的設置,但我認爲這不是需要一個for循環,這樣的事情,使用in_array,應該工作(未經測試,只是表示變更功能):

$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;  
} 

編輯

看起來你還需要類似的東西(根據你的新代碼);我已經錯過添加過濾器:(!is_admin())

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

Thanks @hobo。我會馬上開始研究它! –

+1

順便說一句,看看你試過的東西,我不確定你爲什麼改變'comma_taxonomies_filter'來使用'$ custom_taxonomy_type_array',因爲原始函數沒有使用'$ custom_taxonomy_type'。我不認爲需要修改。並且爲了將來的參考,當「結果在空白頁面中」時,檢查的地方是你的PHP日誌 - 那裏通常會有一個錯誤信息。 – Hobo

+0

@GuillermoCarone - 任何更新? – Hobo

1

如果{//確保過濾器只稱爲前端

$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); 

我的代碼是工作 「|| 「