I'm in drupal 7. Let's say I have a block on a website page. The website page is a node that has taxonomy terms. How do I programatically get the taxonomy terms of the website page node from my block?


You should be able to use menu_get_object() to get the node object if the block is being displayed on a node page. Then, with the $node object you can retrieve terms for the node.

if ($node = menu_get_object()) {
  $query = db_select('taxonomy_index', 'r');
  $t_alias = $query->join('taxonomy_term_data', 't', 'r.tid = t.tid');
  $v_alias = $query->join('taxonomy_vocabulary', 'v', 't.vid = v.vid');
  $query->fields( $t_alias );
  $query->condition("r.nid", $node->nid);
  $result = $query->execute();
  $terms = array();
  foreach ($result as $term) {
    $terms[$term->tid] = $term;

Just remember that that if caching is enabled in your block it should be per-page caching, otherwise you could get unexpected results.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.