我试图找出如何分离自定义的post类型分类法。
$terms = get_the_terms( $post->ID , array( \'commitments\', \'type\' ) );
foreach ( $terms as $term ) {
$term_link = get_term_link( $term, array( \'commitments\', \'type\' ) );
if( is_wp_error( $term_link ) )
continue;
echo \'<a href="\' . $term_link . \'">\' . $term->name . \'</a>\';
}
每个分类法都正确显示。然而,我不能用逗号分隔它们。它显示“TaxonomyATaxonomyB”,但我想显示为“TaxonomyA,TaxonomyB”
怎么做?或者还有其他方法吗?
谢谢
最合适的回答,由SO网友:Dexter0015 整理而成
您可以使用计数器确定是否需要添加逗号:
$terms = get_the_terms( $post->ID , array( \'commitments\', \'type\' ) );
// init counter
$i = 1;
foreach ( $terms as $term ) {
$term_link = get_term_link( $term, array( \'commitments\', \'type\' ) );
if( is_wp_error( $term_link ) )
continue;
echo \'<a href="\' . $term_link . \'">\' . $term->name . \'</a>\';
// Add comma (except after the last theme)
echo ($i < count($terms))? ", " : "";
// Increment counter
$i++;
}