使用get_the_terms
而不是the_terms
.
http://codex.wordpress.org/Function_Reference/get_the_terms
像这样的事情应该可以做到:
$terms = get_the_terms( get_the_ID(), \'ehp_volumes\' );
$term_count = count( $terms );
if ( $term_count > 0 && ! wp_error( $terms ) ) {
$terms = array_values( $terms ); // reset keys for easier looping. by default the key matches the term id
for( $i = 0; $i <= $term_count; $i++ ) {
$output.= $terms[$i]->name;
$output.= ( $i < $term_count-1 ? \', \' : null ); // add commas between terms, but not to the last one
}
echo $output;
unset( $output );
}
unset( $terms, $term_count );
以下是基于评论的更新建议(需要能够订购条款说明)。要完成此用途
wp_get_object_terms
而不是
get_the_terms
因为它接受第三个参数,允许您对结果进行过滤和排序。
实际上,我更喜欢这个——简单得多。
$terms = wp_get_object_terms(
$post->ID,
\'ehp_volumes\',
array(
\'orderby\' => \'name\',
\'order\' => \'DESC\',
\'fields\' => \'names\'
)
);
echo ( ! is_wp_error( $terms ) ? implode( \', \', $terms ) : \'null\' );
unset( $terms );