Search This Blog

Loading...

Wednesday, November 16, 2011

Drupal: Get vocabulary id by term id

In Drupal 6, if you know the taxonomy term ID, you can get the vocabulary ID by using the following code:
$term = taxonomy_get_term($tid);
$vid = $term->vid;
If you have a node ID, then you can use the following code to get the vocabulary ID of all the taxonomy terms associated with the node using the following code:
$node = node_load($nid);
$vids = array();

if (!empty($node->taxonomy) {
  foreach ($node->taxonomy as $tid => $term) {
    $vids[] = $term->vid;
  }
}
In Drupal 7, the code would be the following:
$term = taxonomy_term_load($tid);
$vid = $term->vid;
In Drupal 7, the node property $node->taxonomy doesn't exist anymore. Instead, there is$node->field_<vocabulary_name>, which is an array with two different structures.

0 comments:

Post a Comment

LinkWithin

Related Posts with Thumbnails