在向最終用戶顯示值之前,我有一段代碼用於替換名爲「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);
}
}
Thanks @hobo。我會馬上開始研究它! –
順便說一句,看看你試過的東西,我不確定你爲什麼改變'comma_taxonomies_filter'來使用'$ custom_taxonomy_type_array',因爲原始函數沒有使用'$ custom_taxonomy_type'。我不認爲需要修改。並且爲了將來的參考,當「結果在空白頁面中」時,檢查的地方是你的PHP日誌 - 那裏通常會有一個錯誤信息。 – Hobo
@GuillermoCarone - 任何更新? – Hobo