Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
add comment

3 Answers

up vote 6 down vote accepted

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.

share|improve this answer
    
This didn't work. I suspect it's because I'm using a custom taxonomy and not categories. –  tnorthcutt Feb 2 '11 at 22:07
1  
But using the_taxonomies() instead did work! –  tnorthcutt Feb 2 '11 at 22:08
    
Hey, at least it was in the ballpark! :-) –  Peter Rowell Feb 3 '11 at 2:06
    
If you'll edit your answer to reflect what works, I'll accept it as correct. Please add the correct usage after what you have, preserving your current answer for context. Thanks! –  tnorthcutt Feb 3 '11 at 13:56
add comment

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 ) );
share|improve this answer
    
Neither of those seem to work. Regardless, wouldn't those just loop through the terms for a single taxonomy? I'm trying to loop through all the taxonomies... I do see that get_terms can take an array, but that didn't seem to work. Here's what I tried: get_terms( array( 'neighborhood', 'price'), array( 'hide_empty' => false ) ); –  tnorthcutt Feb 2 '11 at 22:04
    
Ahh, I see. I misread the question. My mistake. –  Norcross Feb 4 '11 at 2:36
    
Got it - you were thinking just looping through the terms for a single taxonomy? In that case, great answer ;). –  tnorthcutt Feb 4 '11 at 4:38
add comment

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>';
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.