How to control Block visibility by Vocabulary ID

Last modified: April 8, 2008 - 22:25

Ever wanted to display a block that appears in nodes that fall under one vocabulary? Look no further.

Edit the variable $desired_vocab in the code below to your desired vocabulary id to display the block in. The vocabulary id can be found by clicking on vocabularies and placing your mouse over the edit link.

This code works with both taxonomy and category modules.

<?php
 
// This snippet returns TRUE if the node we are
  // currently viewing is tagged with a term which belongs
  // to the 'desired_vocab' and we are not in edit mode (arg(2)).

  // put here the vocabulary ID you're interested in
 
$desired_vocab = 1;

  if (
arg(0) == 'node' and is_numeric(arg(1)) and arg(2) == FALSE ) {
   
// Yes, we're viewing a node in view mode.

   
$node = node_load(arg(1)); // cached
    // If the term does not exist we're done
   
if (is_array($node->taxonomy)) {
      foreach (
$node->taxonomy as $term) {
        if (
$term->vid == $desired_vocab) {
          return
TRUE;
        }
      }
    }
  }
  return
FALSE;
?>

Block visibility by term within a same taxonomy
You can just change the vid to tid. Both fields are from the same table, term_data, where vid is the vocabulary ID and tid is the term ID.

So the code for that is:

<?php
 
// This snippet returns TRUE if the node we are
  // currently viewing is tagged with a term which is
  // the 'desired_term' and we are not in edit mode (arg(2)).

  // put here the term ID you're interested in
 
$desired_term = 59;

  if (
arg(0) == 'node' and is_numeric(arg(1)) and arg(2) == FALSE ) {
   
// Yes, we're viewing a node in view mode.

   
$node = node_load(arg(1)); // cached
    // If the term does not exist we're done
   
if (is_array($node->taxonomy)) {
      foreach (
$node->taxonomy as $term) {
        if (
$term->tid == $desired_term) {
          return
TRUE;
        }
      }
    }
  }
  return
FALSE;
?>

if you want to display a

Ainur - July 25, 2008 - 16:47

if you want to display a block by Vocabulary ID in taxonomy listing, having one or more terms (ex. www.italia-ru.it/taxonomy/term/64+81+82+83+84+85+86+87+88+89+90+91+92), use this code:

<?php
$match
= FALSE;

// put here the vocabulary ID you're interested in
$vid = 2;

  if (
arg(0) == 'taxonomy' && arg(1) == 'term') {
 
$str_tids = arg(2); 
 
$terms = taxonomy_get_tree($vid);
  foreach (
$terms as $term ) {
   
$items[] = $term->tid;
  }
 
 
$terms = taxonomy_terms_parse_string($str_tids); 
 
 
//browse each value
 
foreach ($terms['tids'] as $k => $v) {
    if (
in_array($v, $items)) {
     
$match = TRUE;
    }
  }
}
return
$match;
?>

Shorter version for checking term ID

marcvangend - February 27, 2009 - 10:03

The code for checking a Term ID can be shorter:

<?php
 
// This snippet returns TRUE if the node we are
  // currently viewing is tagged with a term which is
  // the 'desired_term' and we are not in edit mode (arg(2)).

  // put here the term ID you're interested in
 
$desired_term = 59;

  if (
arg(0) == 'node' and is_numeric(arg(1)) and arg(2) == FALSE ) {
   
$node = node_load(arg(1));
    return (bool)
$node->[$desired_term];
  }
?>

You don't need to return FALSE explicitly; if nothing is returned, the block will not show.

Should be $node->taxonomy[$desired_term]

Mark Birbeck - July 7, 2009 - 17:25

Thanks for this, but the return value needs to by an item in the $node->taxonomy array.

 
 

Drupal is a registered trademark of Dries Buytaert.