I have a custom taxonomy called services. Each service term will have a description. I am using get_the_term_list to show all the terms attached to each post. When hovering over the term link in the front-end I would like to show the term description just like WordPress does within the back-end when you hover over any link.
1 Answer
If you mean to add the description of the term as a title attribute which will show on hover then you can "fork" get_the_term_list
function to a version that would add the title attribute, something like this should work:
function get_terms_with_desc( $id = 0 , $taxonomy = 'post_tag', $before = '', $sep = '', $after = '' ){
$terms = get_the_terms( $id, $taxonomy );
if ( is_wp_error( $terms ) )
return $terms;
if ( empty( $terms ) )
return false;
foreach ( $terms as $term ) {
$link = get_term_link( $term, $taxonomy );
if ( is_wp_error( $link ) )
return $link;
$term_links[] = '<a href="' . esc_url( $link ) . '" rel="tag" title="'.esc_attr( $term->description ).'">' . $term->name . '</a>';
}
$term_links = apply_filters( "term_links-$taxonomy", $term_links );
return $before . join( $sep, $term_links ) . $after;
}