WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm using the code below to show the featured image and link for the first post in each custom taxonomy for a custom post type. I'd like to display the name of the custom taxonomy next to each image, but despite best efforts I can't find the way to do this. I only want to show the current taxonomy that is being queried. Any ideas?

        $post_type = 'prints';

        // Get all the taxonomies for this post type
        $taxonomies = get_object_taxonomies( (object) array( 'post_type' => $post_type ) );

        foreach( $taxonomies as $taxonomy ) : 

            // Gets every "category" (term) in this taxonomy to get the respective posts
            $terms = get_terms( $taxonomy );

            foreach( $terms as $term ) : 

                $posts = new WP_Query( "taxonomy=$taxonomy&term=$term->slug&posts_per_page=1" );

                if( $posts->have_posts() ): while( $posts->have_posts() ) : $posts->the_post(); ?>
                    <div class="cat-preview">
                        <a href="<?php esc_url( the_permalink() ); ?>"><?php the_post_thumbnail ('medium'); ?></a>
                        <h2><!-- Looking to add custom taxonomy title here --></h2>
                endwhile; endif;


share|improve this question
Do you mean name of taxonomy or term in taxonomy? – Rarst Sep 16 '13 at 12:29
Sorry, yes, the name of the term in the taxonomy. G.M. has given a nice simple answer below which works perfectly. – jasonbradberry Sep 16 '13 at 12:37
up vote 2 down vote accepted

The name of the current taxonomy term is in the name property of the term object, so, in your loop just echo it:

if( $posts->have_posts() ): while( $posts->have_posts() ) : $posts->the_post(); ?>

<div class="cat-preview">
  <a href="<?php esc_url( the_permalink() ); ?>"><?php the_post_thumbnail ('medium'); ?></a>
  <h2><?php echo $term->name; ?></h2>
</div><?php endwhile; endif;
share|improve this answer
You are a legend. Perfectly simple when you know how. Thanks! – jasonbradberry Sep 16 '13 at 12:36

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.