37

A lot of our stuff has uris formatted like site/taxonomy/XX, where XX is an integer.

I found a lot of questions like "how to get the TID from the name?", but I'd like to get the name from the TID. I'm trying to put together a breadcrumb script and everything is great except for these cases where I'm getting a trail like "home > term". Instead I want to have it like "home > <TERM NAME>".

How can I do so?

1
  • Great answers. Only for info: For those who want to do the opposite - i.e. get tid from term name, there is a Q&A for that too: drupal.stackexchange.com/questions/36594/… Hope this is helpful as Search Engines when looking for answers don't seem to understand the ordering of what one needs in terms of 'from' and 'to'. Commented Mar 12, 2018 at 11:46

5 Answers 5

99

If you're using Drupal 7 you can use taxonomy_term_load()

$term = taxonomy_term_load($tid);
$name = $term->name;

If you've got a bunch of term IDs you can save having to run a single query for each load by using taxonomy_term_load_multiple():

$tids = array(1, 2, 3);
$terms = taxonomy_term_load_multiple($tids);

foreach ($terms as $term) {
  $name = $term->name;
}

If you're stuck using Drupal 6 you can use taxonomy_get_term():

$term = taxonomy_get_term($tid);
$name = $term->name;

There's no multiple-load option for Drupal 6 that I know of unfortunately.

3
  • thank you very very much wish i could extra plus for the version support :D
    – Brodie
    Commented Jul 23, 2012 at 16:09
  • additional question: I think this along the same lines, is it pretty easy to set it so the urls read site/<term name> ? or would i have to set that for each individual term?
    – Brodie
    Commented Jul 23, 2012 at 16:10
  • 3
    You can do it on a term-by-term basis but that's pretty laborious. The pathauto module was written just for this sort of situation, you can use it to define patterns to auto-assign aliases to terms/content/users/etc.
    – Clive
    Commented Jul 23, 2012 at 16:26
13

In Drupal 8, you can get the name of taxonomy terms this way:

$term = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->load($tid);

$name = $term->label();

Or to load multiple:

$terms = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadMultiple($tids);

foreach($terms as $term) {
  $name = $term->label();
}
2
  • I get this error - Notice: Undefined property: Drupal\Core\Entity\Plugin\DataType\EntityReference::$target
    – Diana
    Commented Apr 7, 2017 at 13:39
  • This worked for me. Thanks
    – Binny
    Commented Sep 10, 2020 at 21:05
5

The following function returns the name of a taxonomy term based on its tid:

function get_term($tid) {
  return db_select('taxonomy_term_data', 't')
  ->fields('t', array('name'))
  ->condition('tid', $tid)
  ->execute()
  ->fetchField();
}
3
  • 1
    It's not advisable to use database functions when you can achieve the same thing with an API function.
    – Kartagis
    Commented Jul 8, 2015 at 7:57
  • 4
    In some case it's better to use database function because the API functions load a big volume of data
    – houmem
    Commented Oct 25, 2015 at 20:17
  • Also useful when taxonomy access permission modules restrict access
    – Dimetry
    Commented Mar 18, 2017 at 18:48
3

For D7:

$term = taxonomy_get_term_by_name($term_name, $vocab_name);  
  foreach($term as $key => $data) {  
    $tid = $data->tid;  

    dpm($tid);  

  }  

For D8:

$term_name = \Drupal\taxonomy\Entity\Term::load(2)->get('name')->value;  
dpm($term_name);  
1

It is possible to obtain the name of the term in Drupal 8 in the following way:

//Obtain the term.
$tid=1;
$term= taxonomy_term_load($tid);

//get the field name
$term->label();

//or
$term->get('name')->value;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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