I’m using a custom post type and custom taxonomies, and using the following to display the terms of the taxonomies on the post page:
the_terms( $post->ID, 'taxname', 'beforetext', ', ');
Is there a way to loop through all the taxonomies assigned to this post and display the terms in an unordered list, rather than a separate line in functions.php for each taxonomy? I suspect that this can be done with a foreach loop, but I don’t know the right syntax.
This answer was a joint effort with tnorthcutt so I made it community wiki.
There are several interesting functions available:
the_taxonomies()
, which callsget_the_taxonomies()
, which in turn callsget_object_taxonomies()
andwp_get_object_terms()
.the_taxonomies()
is a documented template tag and is a light wrapper around a call toget_the_taxonomies()
, which is undocumented.get_object_taxonomies()
returns a list of associated taxonomies (as simple names or as objects), which could be quite useful in certain situations. It’s documented, but it’s easy to miss this function because nothing links to it in the Codex. I only found it by perusingwp-includes/taxonomy.php
. There is an undocumented wrapper function around this,get_post_taxonomies()
, which defaults to the current post.wp_get_object_terms()
does most of the heavy lifting. It has two very interesting parameters: an array(!) of object ids and an array(!) of taxonomy names. It ends up generating a moderately evil-looking SELECT and returns an array of terms (as names, objects, or … read the docs) for the given object(s) within the given taxonomy(ies).Should you need more complex formatting than is available via
the_taxonomies()
, knowing about these “inner” functions should prove useful.I think what you’re looking for is get_the_term_list . Give this a shot
otherwise, this function may pull them. just not sure how they’ll spit out in terms of markup
This is working quite well on any custom taxonomy (you don’t have to specify taxonomy name).
You might need
global $post;
or not, depending on where you are planning to place this code.