在向最终用户显示值之前,我有一段代码用于替换名为“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