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

eojthebrave’s picture

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?

emdalton’s picture

That's it-- Thanks!!!

theamazingdrew’s picture

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.

alison’s picture

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:

<?php 

$desired_terms = array(8); /* enter the numeric taxonomy term ID(s), sep. by commas */

if ( (arg(0) == 'node') && is_numeric(arg(1)) && arg(2) == FALSE ) {
    /* are we on a node in "view" mode? (versus "edit" mode, etc.) */
   
    $node = node_load(arg(1)); 
/* check the node's taxonomy term(s) */
        foreach ($node->taxonomy as $term) {
/* if any of the terms are in the "desired" list, we have a winner! */
        if ( in_array($term->tid, $desired_terms) ) {
           return TRUE;
        }
   }
}
/* ...and if not, please enjoy these nice party gifts, ie, not this banner */
return FALSE;
?>

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:

<?php 

$desired_terms = array(8, 10, 4, 5, 2, 17, 21); /* term ID(s) sep. by commas -- all the TIDs from the other banner blocks */

    /* are we on a node in "view" mode? (is this NOT the front page? -- haven't finished this part yet, but would be nice) */
if ( (arg(0) == 'node') && is_numeric(arg(1)) && arg(2) == FALSE ) {
   
    $node = node_load(arg(1)); 

/* does this node have NO terms at all? great, you're in, and we're done with you for today */
    if ( !($node->taxonomy) ) {
        return TRUE;
    }
/* otherwise, carry on & check the taxonomy terms */
    if ($node->taxonomy) {
        foreach ($node->taxonomy as $term) {
/* if NONE of the terms are in the "desired" list, great, welcome to the island of misfit toys! */
            if ( !(in_array($term->tid, $desired_terms)) ) {
               return TRUE;
            }
        }
    }
}
/* ...otherwise, please, please don't appear */
    return FALSE;
?>

Hope this is helpful, and good luck!

sirwhistler’s picture

I have been searching for a nice way to do this for hours. Thanks for this easy solution!

starfs’s picture

does this work in drupal 7?

dialn’s picture

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