How to control Block visibility by Vocabulary ID
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;
?>