Display wordpress custom taxonomy with separators and title attribute

I was looking and having trouble finding a function that would not only return my custom taxonomy terms, but with the title description attribute so that when people hover over the link they get some more info.

Get_the_terms_list function did half of what I wanted – I got the custom taxonomy plus a separator if more than one, but no description:

			<?php echo get_the_term_list( $post->ID, 'film-year', '', ', ', '' ); ?>

So here is my solution:

			<?php
			$terms = get_the_terms( $post->ID, 'film-year' );

			if ( $terms && ! is_wp_error( $terms ) ) : 

				$year_links = array();

				foreach ( $terms as $term ) {
				    $term_name = $term->name; // get the ID of the term
							$year_links[] = '<a href="'.get_term_link($term).'" title="View all films from '.$term_name.'">'.$term_name.'</a>';
				   
				}

				$filmyearlinks = join( ", ", $year_links );
			?>

			<p>
				Film year: <span><?php echo $filmyearlinks; ?></span>
			</p>

			<?php endif; ?>

Enjoy!

Leave a Reply