WordPress标签中的逗号

时间:2019-04-11 03:48:42

标签: wordpress

我要在城市,县和州名称的标记名称中显示逗号。当前,即使将它们输入到标签编辑器中,它们也是分开的(以后在尝试将它们添加到帖子中时,它们是分开的)。

我找到了一篇文章,概述了如何通过使用“-”作为逗号的占位符并使用查找和替换将其转换为逗号来欺骗系统。 https://www.saotn.org/display-commas-wordpress-tags/但是,将代码放入functions.php并使用名称中带有“-”的标记时,不会发生任何事情。我仍然看到“-”而不是逗号。

// 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' );
}

0 个答案:

没有答案