0
votes

I want to have commas display in tag names for city, county, and state names. Currently, they are separated, even when entering them into the tag editor (they later separate when trying to add them into a post).

I found an article that outlines how to cheat the system by using "--" as a placeholder for the comma and using a find and replace to turn it into a comma. https://www.saotn.org/display-commas-wordpress-tags/ However, when placing the code into functions.php and using a tag with "--" in its name, nothing happens. I see "--" still instead of the comma.

// filter for tags (as a taxonomy) with comma
//  replace '--' with ', ' in the output - allow tags with comma this way
if( !is_admin() ) { // make sure the filters are only called in the frontend
    $custom_taxonomy_type = 'location'; // 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_' . $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' );
}
You are trying to find / replace '--' (2 dashes) instead of '-' (one). - WhereDidMyBrainGo
What is the problem with that? As far as I can tell, it's a string that's never used so it shouldn't create any conflicts. I'm not trying to change it in the slug, just the way it displays on the front end (Boston, MA instead of Boston-- MA). - Stronghammer
Let me run this by you, I've been entering them in as tags instead of as a custom taxonomy, but when I enter it in as a custom taxonomy, nothing shows up from that custom taxonomy in the front end either. Is there an easier solution to just keep the comma in between place names? - Stronghammer