WordPress Taxonomies: Displaying terms as text

Mar 29, 2011

While using custom post types on a recent site, I ran into a situation where I wanted to list the terms(taxonomies) associated to a post. The function get_the_terms worked great, but I wanted to list the terms as text and not URLs. The first thing that came to mind was to use “strip_tags”, but that just felt dirty… so here’s a simple solution to display a post’s terms as text and not URLs.

<?php function get_language_terms($args) {
            // Get terms for post
            $terms = get_the_terms( $post->ID , $args );
 
            if ( !empty( $terms ) ):
                // Loop over each item since it's an array
                foreach( $terms as $term ) {
                    // Print the name method from $term which is an OBJECT
                    echo $term->name;
                    // Get rid of the other data stored in the object, since it's not needed
                    unset($term);
           } endif;
 
} ?>

Then place this inside the loop and specify the argument for which taxonomy you are wanting to list terms for.

<?php get_language_terms('taxonomy_slug_goes_here'); ?>