I need to get the terms a node has. I found out that you can use this:
$result = field_view_field('node', $node, 'field_tags', array('default'));
But this prints a rendered list. I need the names, or tids.
How do I separate them?
Drupal Answers is a question and answer site for Drupal developers and administrators. It only takes a minute to sign up.
Sign up to join this communityI need to get the terms a node has. I found out that you can use this:
$result = field_view_field('node', $node, 'field_tags', array('default'));
But this prints a rendered list. I need the names, or tids.
How do I separate them?
with taxonomy_node_get_terms() function (D6) you can get 'terms from node'. one example:
<?php
$terms = taxonomy_node_get_terms($node);// get term from nodes
rsort($terms); //rsort() just sort an array in reverse order
$myterm = $terms[0]->name; //get a value
echo $myterm; //show value
?>
The structure of a taxonomy field in a node is similar to the following one:
The first array index is the identifier of the language associated with the taxonomy, or "und" (the value of the constant LANGUAGE_NONE) if the taxonomy term doesn't have any associated language.
This is how I got it working:
$terms = taxonomy_node_get_terms($node);
$i = 0;
foreach ($terms as $term){
$tid = $term->tid;
$name = $term->name;
// show the terms
print $name.'<br>';
}
Searching on api.drupal.org I see there is a field_get_items('node', $node, 'field_tags');
that you could use.
You can also inspect the $node object and get the values directly from there. To inspect the $node
object I recommend you install the devel module and then use the dpm or krumo function like:
krumo($node);