I’ve been modifying a theme I bought awhile ago, adding in a custom post type and corresponding taxonomies. I thought it’d be a cool idea to display an image representing one of my taxonomies instead of just the text or a link to the category. So after trying just about every wp function having to do with getting terms, I finally found my answer and decided to share it so you won’t have to spend the hours I did figuring this out.
I found this on CSS Tricks which continually proves to be a great source for finding answers to my problems.
So here’s the a generic version of the code I used:
<?php $terms_as_text = get_the_term_list( $post->ID,'my_taxonomy'); if (!empty($terms_as_text)) $check_img = strip_tags($terms_as_text); ?>
<a href="<?php echo get_post_meta($post->ID, 'link_from_custom_field', true); ?>">
<?php if($check_img == 'Matches Term 1') { ;?>
<img src="<?php bloginfo('stylesheet_directory'); ?>/images/img_of_term1.jpg" alt="<?php echo $check_img; ?>" title="<?php echo $check_img; ?>" />
<?php } elseif ($check_img == 'Matches Term 2') { ;?>
<img src="<?php bloginfo('stylesheet_directory'); ?>/images/img_of_term2.jpg" alt="<?php echo $check_img; ?>" title="<?php echo $check_img; ?>" />
<?php } elseif ($check_img == 'Matches Term 3') { ;?>
<img src="<?php bloginfo('stylesheet_directory'); ?>/images/img_of_term3.jpg" alt="<?php echo $check_img; ?>" title="<?php echo $check_img; ?>" />
<?php } ?>
</a>
get_the_term_list(); gets the taxonomy terms from my current post ($post->ID) and from a selected taxonomy (‘my_taxonomy’). After checking to make sure the taxonomy isn’t empty, it strips it of the anchor. Because I wanted to display a different image based on which taxonomy term was being used, I set the stripped taxonomy term to $check_img and then checked it against each of three different terms in order to display the appropriate image.
Also, here’s a reference that I made that helps explains the difference between the various term-related functions:
get_terms vs the_terms vs get_the_terms etc.
get_terms(); — Retrieve the terms in taxonomy or list of taxonomies.
<?php get_terms( $taxonomies, $args ) ?>
get_term_link(); — Returns permalink for a taxonomy term archive.
<?php get_term_link( $term, $taxonomy ); ?>
get_the_terms(); — Retrieve the terms of the taxonomy that are attached to the post.
<?php get_the_terms( $id, $taxonomy ); ?>
get_the_term_list(); — Returns an HTML string of taxonomy terms associated with a post and given taxonomy. Terms are linked to their respective term listing pages.
<?php get_the_term_list( $id, $taxonomy, $before, $sep, $after ) ?>
the_terms(); — Displays the terms of a custom taxonomy.
<?php the_terms( $id, $taxonomy, $before, $sep, $after ); ?>
wp_get_object_terms(); — Retrieves the terms associated with the given object(s), in the supplied taxonomies.
<?php wp_get_object_terms( $object_ids, $taxonomies, $args ) ?>
And there are a ton more, but those are the ones I kept referencing, so hopefully that will be a help to you as well.