Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
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!
Comments
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/6
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 thewhile
loop too many times, but is bad style and you should not do it if you have enough computing power.By code is:
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
$displayTermID
or 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.
$displayTermID
must 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.
works for nodes...
as you indicated, but not on the taxonomy index listing page.
Here is a suggestion on how to get this to show the block on the index page as well:
doesn't show on all index pages
Can anyone assist in modifying this snippet so that blocks will show on all sub-term index pages, as well as that of the root ancestor?
Also displaying for taxonomy listings
Hi,
not sure what you mean by "so that blocks will show on all sub-term index pages, as well as that of the root ancestor". Your code (I think, I have not tried it) should show the block in the following cases:
- When a node is displayed that has a category term with the ID
$displayTermID
assigned to it.- When a node is displayed that has a category term
T
assigned to it, such thatT
has an ancestor term with the ID$displayTermID
.- When a category listing for the term with the ID
$displayTermID
is displayed.I think, what you want is to also display the block when a category listing for a term is displayed that has an ancestor with the ID
$displayTermID
. I have removed the Drupal 6 installation and replaced it with it with Drupal 5.7 because of the serious lack of modules for Drupal 6 makes is not very useful for me (e.g. see discussion at http://drupal.org/node/259586, but that is unrelated). Because of this I cannot really show you a WORKING Drupal 6 snippet here, but here is what you need to do in general:Found a working method (D6)
I tried the code above but it didn't work, the code below should work though.
--
How to override HTML in Drupal 6
Here is a TESTED simple method:
Hi, and thank you for the small examples. Unfortunately, they did not work on my install. Here is a version which worked for me:
basically, the difference with prior art is:
Only on node pages
In my case, this code show the block on every node page, but not on the taxonomy lists or index pages for the term in question. I tried also removing
if ( arg(0) == 'node' and is_numeric(arg(1)) and arg(2) == FALSE )
but still had no success.But this worked for me:
One issue is that the block is not show on index pages for terms that are subterms (child terms) of the term in question.
Thanks for your help.
See also
See also: