Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
This is a suggestion for another code snippet. If you try to pull the data from a CCK Taxonomy field, you get the Term ID and not the actual term name (the word or phrase or whatever the term is). Here's the code I put together to pull the actual term name.
$tid = $node->field_myfieldname[0]['value'];
$result = db_fetch_array(db_query("SELECT name FROM {term_data} WHERE tid = %d", $tid));
$node_field[0]['value'] = $result[name];
myfieldname - the name of your cck taxonomy field
I'm not a coder, so use this at your own risk.
Edit - with multiple terms:
$tids = $node->field_myfield;
foreach($tids as $key => $val)
{
$tid=$val[value];
$result = db_fetch_array(db_query("SELECT name FROM {term_data} WHERE tid = %d",$tid));
$node_field[]['value']=$result[name];
}
Comments
will it work for multiple
will it work for multiple taxonomy terms ??
I don't know if it will work
I don't know if it will work for tags. The use case it was built for was taxonomy term select lists, so there would have been always only 1 value.
It will probably fetch all the terms, the first term, throw a crazy error, or nothing whatsoever. I've actually discontinued using Taxonomy, so try it with some tags or something and tell us how it works.
I did use it for multiple terms from different vocabularies (i was merging them with subsequent code) and that worked fine, just repeat the code per vocab.
You can simply use print
You can simply use
to get the taxonomy term name. If the field is multivalued, then use foreach loop iteration.
---~~~***~~~---
aac
How to Compute taxonomy term name Drupal 7
Hello
How can I compute taxonomy terms in Drupal 7 and get the term name ?
The following code returns tid but not term name
$entity_field[0]['value'] = array_pop(array_pop(field_get_items($entity_type, $entity, 'field_region')));
- Guillaume -