Displaying custom WordPress taxonomy terms with or without links

WordPress lets you create custom taxonomy terms, a feature that makes WordPress a very good CMS. If you want to display these terms (on a specific post), there are more than one way to do this.

If you want to list the terms associated with a specific post with a link to a term archive, you can use the get_the_term_list() function. This displays the terms (including a term link). To run through the terms, use code similar to this (from the codex):

<?php echo get_the_term_list( $post->ID, 'people', 'People: ', ' ', '' ); ?>

But if you want to display the terms without the links, you can use code similar to this, using wp_get_object_terms():

$people_terms = wp_get_object_terms($post->ID, 'people');
if(!empty($people_terms)){
  if(!is_wp_error( $people_terms )){
    echo '<ul>';
      foreach($people_terms as $term){
      echo '<li>'.$term->name.'</li>';
      }
    echo '</ul>';
  }
}

To display all the terms, regardless of the specific post, you can use the get_terms() function. Code similar to this lists the terms in a unordered list

<?php
$terms = get_terms("people");
 $count = count($terms);
 if ( $count > 0 ){
     echo "<ul>";
     foreach ( $terms as $term ) {
       echo "<li>" . $term->name . "</li>";
     }
     echo "</ul>";
 }
?>

Post a Comment

Your email is kept private. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>