Using a loop to display terms associated with a post

This answer was a joint effort with tnorthcutt so I made it community wiki.

There are several interesting functions available: the_taxonomies(), which calls get_the_taxonomies(), which in turn calls get_object_taxonomies() and wp_get_object_terms().

  • the_taxonomies() is a documented template tag and is a light wrapper around a call to get_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 perusing wp-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.