So I was looking at pulling the associated taxonomy terms for a wordpress project I am working on and needed something like the_tags() for the custom taxonomies I created.
The closest thing to documentation on what I was looking for was the_tags() function
Then I dug into the category-template.php source codewhere the_tags() lived.
I found exactly what I needed. Here is the_tags() function:
function the_tags( $before = 'Tags: ', $sep = ', ', $after = '' ) {
return the_terms( 0, 'post_tag', $before, $sep, $after );
}
‘post_tag’ is just what wordpress used as their key when they decided to create the tags taxonomy, I used ‘ethnicity’, ‘eye’ and ‘hair’ for my new taxonomies.
So what you need to do is call the_terms() and it will do the same for whatever taxonomy you define.
NOTE when you create your taxonomy in your functions.php like so:
function create_taxonomies() {
register_taxonomy( 'ethnicity', 'post', array( 'hierarchical' => false, 'label' => 'Ethnicity', 'query_var' => true, 'rewrite' => true ) );
register_taxonomy( 'eye', 'post', array( 'hierarchical' => false, 'label' => 'Eye Color', 'query_var' => true, 'rewrite' => true ) );
register_taxonomy( 'hair', 'post', array( 'hierarchical' => false, 'label' => 'Hair Color', 'query_var' => true, 'rewrite' => true ) );
}
add_action( 'init', 'create_taxonomies', 0 );
be sure to add a hook for where you want to list the taxonomies:
the_terms( 0, 'ethnicity', 'Ethnicity: ', ', ', ' ' );
the_terms( 0, 'eye', 'Eye Color: ', ', ', ' ' );
the_terms( 0, 'hair', 'Hair Color: ', ', ', ' ' );
the_terms( 0, 'height', 'Height: ', ', ', ' ' );
I am not sure what the difference between using the following would be:
echo get_the_term_list( $post->ID, 'people', 'People: ', ', ', '' );
I think I am cheating by using 0 instead of $post->ID. But the first parameter is the post id, second is the name of the taonomy, third is before, fourth is the seperator, and fifth is what to print after all the terms.
I am not sure how many WP developers are using taxonomies, new in version 2.8, but would be interested in hearing feedback on my approach. When building my taxonomies I used these resources:
Custom Taxonomies in Wordpress 2.8 by Justin Tadlock
Custom Taxonomies – Theme Hybrid Support Forums