我使用wp_post_terms_array输出taxanomy术语,这里是代码:
$post_terms = wp_get_post_terms( $post->ID, 'amenity', array( "fields" => "names" ) );
if($post_terms){
foreach ( $post_terms as $term ) {
echo '<li class="icon-'.strtolower($term).'">'.$term.'</li>';
}
}
我希望$ term输出中的每个单词都与 - 符号连接。
例如:
私人泳池到私人泳池
私人健身房到私人健身房
由于
答案 0 :(得分:1)
由于您似乎正在使用Wordpress,因此正确的功能是sanitize_title
示例:
<?php
$post_terms = wp_get_post_terms( $post->ID, 'amenity', array( "fields" => "names" ) );
if($post_terms){
foreach ( $post_terms as $term ) {
$term = sanitize_title($term);
echo '<li class="icon-'.$term.'">'.$term.'</li>';
}
}
答案 1 :(得分:0)
答案 2 :(得分:0)
只需用带有str_replace的连字符替换空格....
$post_terms = wp_get_post_terms( $post->ID, 'amenity', array( "fields" => "names" ) );
if($post_terms){
foreach ( $post_terms as $term ) {
echo '<li class="icon-'.strtolower($term).'">'.str_replace(" ","-",$term).'</li>';
}
}