32

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?

  • 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'. – therobyouknow Mar 12 '18 at 11:46
90

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.

  • thank you very very much wish i could extra plus for the version support :D – Brodie Jul 23 '12 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 Jul 23 '12 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 Jul 23 '12 at 16:26
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();
}
  • 1
    It's not advisable to use database functions when you can achieve the same thing with an API function. – Kartagis Jul 8 '15 at 7:57
  • 3
    In some case it's better to use database function because the API functions load a big volume of data – houmem Oct 25 '15 at 20:17
  • Also useful when taxonomy access permission modules restrict access – Dimetry Mar 18 '17 at 18:48
4

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();
}
  • I get this error - Notice: Undefined property: Drupal\Core\Entity\Plugin\DataType\EntityReference::$target – Diana Apr 7 '17 at 13:39
1

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);  
0

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, privacy policy and cookie policy

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