Returns a comma delimited list of links for the term or terms for a given taxonomy. This must be used within the loop.
Parameters
$taxonomy: (string) slug of taxonomy. Defaults to “category”
$get_all: (boolean) if set to true, it will get all the terms for this post in this taxonomy. Defaults to false, and only gets the first term found.
File: inc/template-tags.php
function ign_get_terms( $taxonomy = 'category', $get_all = false ) {
$terms = get_the_terms( get_the_ID(), $taxonomy );
if ( $terms && ! is_wp_error( $terms ) ) :
//get first term found
if ( ! $get_all ) {
$term = array_pop( $terms );
$term_links_output = '<a class="term-link ' . $taxonomy . '" href="' . get_term_link( $term->term_id, $taxonomy ) . '">' . $term->name . '</a>';
} //else get all terms with a comma
else {
$term_links = array();
foreach ( $terms as $term ) {
$term_links[] = '<a class="term-link ' . $taxonomy . '" href="' . get_term_link( $term->term_id, $taxonomy ) . '">' . $term->name . '</a>';
}
$term_links_output = join( '<span class="delim">' . __( ', ', 'ben-landel' ) . '</span>', $term_links );
}
return $term_links_output;
endif;
return '';
}
Usage
echo ign_get_terms('genre'); //gets one genre term for this post
echo ign_get_terms('genre', true); //gets all terms under genre for this post