Using a loop to display terms associated with a post

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.

Related posts

Leave a Reply

3 comments

  1. 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.

  2. I think what you’re looking for is get_the_term_list . Give this a shot

    get_the_term_list( $post->ID, 'taxname', '', ', ', '');
    

    otherwise, this function may pull them. just not sure how they’ll spit out in terms of markup

    get_terms( $taxonomy, array( 'hide_empty' => false ) );
    
  3. 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.

    global $post;
    // get the post type
    $post_type = get_post_type( get_the_ID() );
    // go to taxonomies array
    $post_type_taxonomies = get_object_taxonomies( $post_type );
    
    // if we have any taxonomy
    if ( ! empty( $post_type_taxonomies ) ) {
    
        echo '<ul>';
    
        // loop through each of them
        foreach ( $post_type_taxonomies as $taxonomy ) {
            // get terms list for each taxonomy
            $terms = get_the_term_list( get_the_ID(), $taxonomy, '', '</li><li>', ''  );
    
            // show only those terms that are assigned to post 
            if ( $terms ) {
                echo '<li>' . $terms . '</li>';
            }
        }
    
        echo '</ul>';
    }