Use get_the_terms
instead of the_terms
.
http://codex.wordpress.org/Function_Reference/get_the_terms
Something like this should do the trick:
$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 );
Below is an updated suggestion based on comments (need ability to order terms DESC). To accomplish this use wp_get_object_terms
instead of get_the_terms
since it accepts a third parameter allowing you to filter and order the results.
I actually like this one better – much simpler.
$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 );