2

I have the following content types: hotels, and taxonomy terms; in hotels I have a taxonomy field.

How do I retrieve the taxonomy term ID in a node? Is it possible to retrieve the self and parent taxonomy term ID and name?

I have also tried this, but it does not work.

0

2 Answers 2

3

You can use field_get_items(), which returns you the value of a field in the language they would be displayed, or in the language whose code is passed as argument to the function. Generally, you are interested to the field's value in the language that would be used to show the field, and you don't pass any language code to the function.

$items = field_get_items('node', $node, $field_name);

$node contains the node object, and $field_name the name of the taxonomy term field.

What returned in $items is an array containing (possibly) multiple values; each value is an array containing the "tid" index, and the term ID. For a taxonomy term for which users can enter more than a value, this is what the function returns.

screenshot

1

You can use field_info_instance(), as in the following snippet.

$info = field_info_instance('node','enter_your_field_name','enter_content_type_name');
foreach ($info as $ps2 => $key) {
  echo "Info [ $ps2 ] : " . $key . "<br />";
}

In this way, you get the term ID.

2
  • field_info_instance() is not returning any term ID. If you have two different nodes, with two different term IDs, that function would return the same value, as it doesn't ask you to which node you are referring. The ID it is returning is a different ID.
    – avpaderno
    Commented May 21, 2012 at 8:57
  • @kiamlaluno , then how do we get Term id in a node
    – Rafee
    Commented May 21, 2012 at 9:15

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.