I've been using the new Twenty-Ten 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(); ?>
Thanks for posting this. Is there a way to use it outside the loop?
You could use it outside the loop for a specific post, say with
id 123, by calling<?php ucc_get_terms_list( '123' ); ?>.Thanks Jennifer, works a treat and saved me writing my own!
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