WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] List Taxonomy Terms specific to a Post (4 posts)

  1. psnepsts
    Member
    Posted 2 years ago #

    I've got a Custom Post type called "People." I've created a Taxonomy called "Specialties" that is set up hierarchically, such that its terms appear functionally as do Categories. So, when I create a new Person, the various specialties appear in the Edit Post screen and I can easily select what specialties each person has.

    In the template for this taxonomy, I'd like to show each person's entry with a list of the specialties they possess as a link to that taxonomy term's archive page. This functionality is identical to the functionality in the_category(), but I cannot seem to replicate it.

    Here's what I have so far:

    <?php $terms = get_terms("specialties");
     $count = count($terms);
     if ( $count > 0 ){
         echo "<ul>";
         foreach ( $terms as $term ) {
           echo '<li><a href="'.get_term_link($term->slug, 'specialties').'">'. $term->name . "</a></li>";
    		 }
    		 echo "</ul>";
    	 }?>

    This, however, will output a linked list of ALL taxonomy terms regardless of whether this Person is associated with them.

    Any help would be appreciated.

  2. Jackson
    Member
    Posted 2 years ago #

    You want wp_get_post_terms instead of get_terms. See: http://codex.wordpress.org/Function_Reference/wp_get_post_terms

  3. psnepsts
    Member
    Posted 2 years ago #

    Thanks, Jackson!

    For those who might follow this thread, here's the updated code, which works perfectly:

    <?php $terms = wp_get_post_terms($post->ID,'specialties');
     $count = count($terms);
     if ( $count > 0 ){
         echo "<ul>";
         foreach ( $terms as $term ) {
           echo '<li><a href="'.get_term_link($term->slug, 'specialties').'">'. $term->name . "</a></li>";
    		 }
    		 echo "</ul>";
    	 }?>

    This outputs a list of taxonomy terms to which a post in Custom Post Type is associated.

  4. derekshirk
    Member
    Posted 2 years ago #

    What if you wanted to exclude a specific taxonomy from being displayed in the list?

    Specifically, I am using a custom taxonomy of 'current' to control content elsewhere on my site. For the page I'm working on, I need to output the taxonomies for each of the custom post type entries but exclude the 'current' term from being included.

    So if a post is marked as 'current' and 'sustainability' the term list will only display 'sustainability'

Topic Closed

This topic has been closed to new replies.

About this Topic