I have a piece of code which is used to replace double dashes for commas in a wordpress taxonomy called “authors” before displaying values to the end user.
This is working great, but now I need to apply it to several different taxonomies, no just “authors” but also “printers”, “translators”, etc.
My idea was to build an array of taxonomies instead of a single variable and then use a foreach loop to apply the find and replace to each of them, but no matter how I try it I can’t seem to make it work…
Any idea how to make the following code into a foreach loop if $custom_taxonomy_type was an array?
This is the code that is currently working for a single taxonomy.
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);
}
As requested by @Epodax here are the two things I've tried so far. Both result in a blank page:
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);
}
}
and
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);
}
}
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire