How To List A Custom Post Type’s Taxonomies And Terms

I’ve been using the new [Twenty-Ten](http://wordpress.org/extend/themes/twentyten) theme for WordPress on my personal website; it’s great out of the box, but I wanted to display my custom post types and taxonomies in the same format as it uses for categories and tags. The function is broken into two parts: `ucc_get_terms()` returns a multidimensional array of the post’s taxonomy name(s) and each taxonomy’s term links; `ucc_get_terms_list()` gets the taxonomy information and formats the array for display in the template.

In `functions.php`, add the following code:

function ucc_get_terms( $id = '' ) {
  global $post;

  if ( empty( $id ) )
    $id = $post->ID;

  if ( !empty( $id ) ) {
    $post_taxonomies = array();
    $post_type = get_post_type( $id );
    $taxonomies = get_object_taxonomies( $post_type , 'names' );

    foreach ( $taxonomies as $taxonomy ) {
      $term_links = array();
      $terms = get_the_terms( $id, $taxonomy );

      if ( is_wp_error( $terms ) )
        return $terms;

      if ( $terms ) {
        foreach ( $terms as $term ) {
          $link = get_term_link( $term, $taxonomy );
          if ( is_wp_error( $link ) )
            return $link;
          $term_links[] = '<a href="' . $link . '" rel="' . $taxonomy . '">' . $term->name . '</a>';
        }
      }

      $term_links = apply_filters( "term_links-$taxonomy" , $term_links );
      $post_terms[$taxonomy] = $term_links;
    }
    return $post_terms;
  } else {
    return false;
  }
}

function ucc_get_terms_list( $id = '' , $echo = true ) {
  global $post;

  if ( empty( $id ) )
    $id = $post->ID;

  if ( !empty( $id ) ) {
    $my_terms = ucc_get_terms( $id );
    if ( $my_terms ) {
      $my_taxonomies = array();
      foreach ( $my_terms as $taxonomy => $terms ) {
        $my_taxonomy = get_taxonomy( $taxonomy );
        if ( !empty( $terms ) )          $my_taxonomies[] = '<span class="' . $my_taxonomy->name . '-links">' . '<span class="entry-utility-prep entry-utility-prep-' . $my_taxonomy->name . '-links">' . $my_taxonomy->labels->name . ': ' . implode( $terms , ', ' ) . '</span></span>';
      }

      if ( !empty( $my_taxonomies ) ) {
        $output = '<ul>' . "\n";
        foreach ( $my_taxonomies as $my_taxonomy ) {
          $output .= '<li>' . $my_taxonomy . '</li>' . "\n";
        }
        $output .= '</ul>' . "\n";
      }

      if ( $echo )
        echo $output;
      else
        return $output;
    } else {
      return;
    }
  } else {
    return false;
  } 
} 

Within The Loop, replace

				<?php if ( count( get_the_category() ) ) : ?>
					<span class="cat-links">
						<?php printf( __( '<span class="%1$s">Posted in</span> %2$s', 'twentyten' ), 'entry-utility-prep entry-utility-prep-cat-links', get_the_category_list( ', ' ) ); ?>
					</span>
					<span class="meta-sep">|</span>
				<?php endif; ?>
				<?php
					$tags_list = get_the_tag_list( '', ', ' );
					if ( $tags_list ):
				?>
					<span class="tag-links">
						<?php printf( __( '<span class="%1$s">Tagged</span> %2$s', 'twentyten' ), 'entry-utility-prep entry-utility-prep-tag-links', $tags_list ); ?>
					</span>
					<span class="meta-sep">|</span>
				<?php endif; ?>

with

<?php ucc_get_terms_list(); ?>

9 responses to “How To List A Custom Post Type’s Taxonomies And Terms

  1. Thanks for posting this. Is there a way to use it outside the loop?

  2. Thanks Jennifer, works a treat and saved me writing my own!

  3. Hi!

    I tried using this for a site with Roots Theme and I’m getting an error from line 419 in my functions file which appears to be “global $post;”. Any idea on why that might be or how I could fix it?

    • Turns out that I got some weird characters when copying the code from your site… Your presentation of

      <

      pre>-code doesn’t seem to agree with Google Chrome. Thanks 🙂

  4. I was searching for this very long and you helped me very much with your function! But one thing that could be nice to have is the possibility of the “hierarchical” display like it is in wp_list_categories()!

    Best regards,
    Dennis

  5. Help me a lot. Thanks so much

  6. Hi!
    First of all let me thank you!

    You save me a lot of headaches but I would like to ask if it’s possible to lemove the categories listing from the output?

    Thanks!

  7. How do you use ucc_get_terms_list(); print out the category the post in?

    For example. I am trying to setup a wordpress loop on the index page to show the categories name for each post in the CPT.

Leave a Reply