我正在使用
<?php
foreach((get_the_category()) as $category) {
echo $category->cat_name . ', ';
}
?>
现在这显然会在每个“类别”的末尾输出一个逗号,我将如何从列表中的最后一项中删除逗号?
答案 0 :(得分:1)
使用rtrim:
$cats = '';
foreach((get_the_category()) as $category) {
$cats .= $category->cat_name . ', ';
}
echo rtrim($cats, ', ');
答案 1 :(得分:0)
你可以试试这个:
$categories = array();
foreach(get_the_category() as $category) {
$categories[] = $category->cat_name;
}
echo implode($categories, ',');
答案 2 :(得分:0)
这是使用join
函数 -
echo join(",",array_map(function($category){return $category->cat_name;}, get_the_category()));
答案 3 :(得分:-1)
$custom_terms = wp_get_post_terms( $post->ID, 'area');
$seprator = ", ";
$comma = sizeof($custom_terms);
foreach($custom_terms as $custom_term) {
echo $custom_term->name;
if($comma > 1){
echo $seprator;
$comma--;
}
}