Show block by taxonomy term - Drupal 4.7
To show a block only for an specific taxonomy term, here is how to do it:
<?php
$myterm = 163; // replace this id with the term id you want to use.
// This will show on all nodes having this term
if ((arg(0) == 'node') && is_numeric(arg(1))) {
$terms = taxonomy_node_get_terms(arg(1));
foreach($terms as $term) {
if ($term->tid == $myterm) return TRUE;
}
}
// This will show on the index page for that term
if ((arg(0) == 'taxonomy') && (arg(1) == 'term') && (arg(2) == $myterm)) {
return TRUE;
}
// Otherwise
return FALSE;
?>To do the same for more than one taxonomy term use in_array() instead:
<?php
$myterms = array(8, 9, 12, 21); // list the ids of the terms you want
// This will show on all nodes having this term
if ((arg(0) == 'node') && is_numeric(arg(1))) {
$terms = taxonomy_node_get_terms(arg(1));
foreach($terms as $term) {
if (in_array($term->tid, $myterms)) return TRUE;
}
}
// This will show on the index page for that term
if ((arg(0) == 'taxonomy') && (arg(1) == 'term') && (in_array(arg(2), $myterms))) {
return TRUE;
}
// Otherwise
return FALSE;
?>Enjoy!

Show block by taxonomy term - Drupal 6.2
In Dripal 6.2 the parameter to the function
taxonomy_node_get_terms()is not a node ID, but an object.Yestarday was my first day using Drupal and I spent several hours figuring that out.
My solution is based on the code available here: http://api.drupal.org/api/function/taxonomy_node_get_terms_by_vocabulary...
That function is very simular to the one used above (http://api.drupal.org/api/function/taxonomy_node_get_terms/6) and
I am only using the former in order to be slightly more efficient as I already know both - the term and vocabulary IDs. You can adjust the code below if you do not know the vocabulary ID in your case.
Note, this function is executed during every page load. For efficiency reasons I have copied the SQL statement from
taxonomy_node_get_terms_by_vocabulary()directly into my own code. This often saves traversing thewhileloop too many times, but is bad style and you should not do it if you have enough computing power.By code is:
<?php$tid = 1; // vocabulary term ID for whivh to display the block
$vid = 1; // Vocablulary ID (We ignore all but one vocabularies for speed)
if (arg(0) == 'node' && is_numeric(arg(1))) {
$nid = arg(1);
$r = db_query(db_rewrite_sql('SELECT t.tid, t.* FROM {term_data} t INNER JOIN {term_node} r ON r.tid = t.tid WHERE t.vid = %d AND r.vid = %d ORDER BY weight', 't', 'tid'), $vid, $nid);
while ($term = db_fetch_object($r)) {
if ($term->tid == $tid)
return TRUE;
}
}
return FALSE;
?>
Show block by taxonomy term + ancestors - Drupal 6.2
Reg. my previous post (http://drupal.org/node/113651#comment-841679):
In opposite to the examples in the main post it does not show the block on index pages for the term in question - this was intended.
Here is an extended version. It shows a block when the current node belongs to a taxonomy category with the ID
$displayTermIDor any of the sub-categories of that category. For instance if you have a taxonomy like:Windows
- Windows 95
- Windows XP
Linux
- SuSE
- Debian
and you want to display a block on all nodes that belong to all sub-categories of Windows (i.e. "Windows", "Windows 95", "Windows XP") you can use this approach.
$displayTermIDmust be the ID of "Windows".Note, today is my second day of using Drupal, so I apologise if something is wrong, but I think that this version uses the Drupal 6.2 API correctly.
<?php
// Vocabulary term ID for which to display the block:
$displayTermID = 1;
// If current node is not a normal node, then fail-fast:
if (arg(0) != 'node' || !is_numeric(arg(1))) {
// echo "<p>Will return FALSE1.</p>";
return FALSE;
}
// Get all taxonomy terms for current node:
$currNode = new stdClass();
$currNode->vid = arg(1);
$currNodeTerms = taxonomy_node_get_terms($currNode);
// If there are no terms, fail-fast:
if (is_null($currNodeTerms) || 0 == count($currNodeTerms)) {
// echo "<p>Will return FALSE2.</p>";
return FALSE;
}
// For each term of the current node, get all the ancestor terms:
foreach($currNodeTerms as $term) {
$ancestors = taxonomy_get_parents_all($term->tid);
// Chech for each ancestor term whether is is the term we are looking for.
// If it is, return TRUE immediately:
if (!is_null($ancestors) && 0 < count($ancestors)) {
foreach($ancestors as $ancestor) {
if ($displayTermID == $ancestor->tid) {
// echo "<p>Will return TRUE.</p>";
return TRUE;
}
}
}
}
// If we didn't find our term of interest, return FALSE:
// echo "<p>Will return FALSE3.</p>";
return FALSE;
?>