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 trying to list just the first taxonomy term outside the loop.

This is working fine but it lists all terms, and I just need the first - any suggestions?

    $taxonomy = 'taxonomy-name-here';
    $queried_term = get_query_var($taxonomy);

    $terms = get_terms($taxonomy, 'slug='.$queried_term);
    if ($terms) {
      foreach($terms as $term) {
    echo ' ' . $term->name . ' ';

Edit: clarification: the above seems to be returning all categories within the taxonomy, I'm only looking to get the taxonomy category of the current page.

Update: I found a simple answer!:

$categories= wp_get_object_terms($post->ID, 'taxonomy-name');
echo $categories[0]->name;
share|improve this question
ah, it all becomes clear now. you're on a single post page trying to get terms associated with that post, not a taxonomy term page, which is why get_query_var($taxonomy); is empty. – Milo Aug 25 '11 at 17:58
$taxonomy = 'taxonomy-name-here';
$queried_term = get_query_var($taxonomy);
$term = get_term_by( 'slug', $queried_term, $taxonomy );
echo $term->name;
share|improve this answer
Thanks for the reply, but that doesn't seem to return anything for me. – maikunari Aug 24 '11 at 21:19
is this not a taxonomy term page? – Milo Aug 24 '11 at 21:24
No, this is going in the title tag, called only on the taxonomy page though. – maikunari Aug 24 '11 at 21:31
I'm confused. yes it is a taxonomy page, or it isn't? taxonomy page meaning site.com/your-taxonomy-name/the-taxonomy-term/, that's the only context in which get_query_var($taxonomy); is going to contain a taxonomy term. – Milo Aug 24 '11 at 22:29
Sorry about that, yes it is a taxonomy page then. The code I posted above using get_terms() was working so that means your code should work too right? – maikunari Aug 24 '11 at 22: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.