By emdalton on
My users need to be able to identify a page by taxonomy term and have a predefined menu appear on the page. (For various reasons, they are not happy with automatically constructed menus of nodes with the same term.) I had this working in D5, but in D6 this php snippet apparently always returns "true." I've checked the term ids, and they haven't changed. Any suggestions?
$myterms = array(39, 59, 85, 114); // 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;
I tried using the patched version of blockterm (http://drupal.org/node/248247), but got a white screen whenever I tried to configure a block.
Does anyone know of a better way to make display of a block (including a menu) dependent on the terms selected for the node?
Comments
In Drupal 6 the function
In Drupal 6 the function taxonomy_node_get_terms takes a full node object as the first argument not just the nid like Drupal 5 does. http://api.drupal.org/api/function/taxonomy_node_get_terms/6.
If you replace
$terms = taxonomy_node_get_terms(arg(1));with
$terms = taxonomy_node_get_terms(node_load(arg(1)));Does it work?
That's it-- Thanks!!!
That's it-- Thanks!!!
Explanation
For those of us only peripherally familiar with php, can someone step through what the arguments do in this statement and how to change it to apply to our web pages? I'm looking to do the same thing, but am not familiar enough with php to customize this for my web page.
If you still need help...
Not sure if you've already figured this out, but, just in case (or just in case someone else stumbles across this page), here's some guidance:
http://drupal.org/node/136029
And, using that page, a couple other pages under PHP block visibility settings (http://drupal.org/node/60317), and this very page as well, I ended up with the code snippets below to display different banners on pages based on taxonomy term(s) -- and a default banner on "the rest" of the pages on this site (in case the examples are helpful for anyone; they usually are for me).
**The code snippets below go in the "Page specific visibility settings" for blocks, when the option "Show if the following PHP code returns TRUE" is selected.
I used variations of the following code for each of my category-related banners to make each one display on pages tagged with a specific taxonomy term or set of terms:
Then, after doing that for each category-related banner block, I used the following snippet to make my "default banner" display on any pages that didn't fit into any of the categories I'd made custom banners for:
Hope this is helpful, and good luck!
Thanks
I have been searching for a nice way to do this for hours. Thanks for this easy solution!
drupal 7
does this work in drupal 7?
display menus on pages with specific taxonomy terms
I also need this function in Drupal 7, not how I tested the module context, blockterm but the block does not appear anyone has gotten in D7