The Stack Overflow podcast is back! Listen to an interview with our new CEO.
3 added 138 characters in body
source | link

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;

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.

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;
2 added 161 characters in body
source | link

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.

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 . ' ';
      }
    }

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.

1
source | link

Return single taxonomy term outside the loop

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 . ' ';
      }
    }