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

meet.h.thakkar’s picture

will it work for multiple taxonomy terms ??

StratisFear’s picture

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.

aac’s picture

You can simply use

print check_plain($node->field_myfieldname[0]['view']);

to get the taxonomy term name. If the field is multivalued, then use foreach loop iteration.

---~~~***~~~---
aac

creatile’s picture

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 -