tags:

views:

52

answers:

1

Hi,

I'm just trying to restrict block visibility to nodes that have a certain taxonomy ID. I'm using this snippet...:

<?php
  $term_id_to_trigger_show_block = 109;

  if ((arg(0) == 'node') && is_numeric(arg(1))) {
    $terms = taxonomy_node_get_terms(arg(1));
    foreach($terms as $term) {
      if ($term->tid == $term_id_to_trigger_show_block) {
         return TRUE;
      }
    }
  }
?>

...but I get no joy, the block remains hidden on the relevant nodes.

Any ideas?

Cheers

+2  A: 

It looks like in drupal6 taxonomy_node_get_tree() takes a node rather than a nid.

The easiest way to change your code is:

<?php
  $term_id_to_trigger_show_block = 109;

  if ((arg(0) == 'node') && is_numeric(arg(1))) {
    $node = node_load(arg(1));
    $terms = taxonomy_node_get_terms($node);
    foreach($terms as $term) {
      if ($term->tid == $term_id_to_trigger_show_block) {
         return TRUE;
      }
    }
  }
?>

node_load() caches nodes in memory so it won't be a big performance hit.

But wait! you may be able to refine this even further...

menu_get_item() will get the currently loaded menu item when the node object is loaded it will call taxonomy_node_get_terms(). So you can simplify to:

<?php
  $term_id_to_trigger_show_block = 109;
  $object = get_menu_item();

  if (isset($object->taxonomy)) {
    foreach($object->taxonomy as $term) {
      if ($term->tid == $term_id_to_trigger_show_block) {
         return TRUE;
      }
    }
  }
?>

This will get other object types wich have a taxonomy object which could cause some confusion, if so stick the arg(0) == 'node' back in.

Jeremy French
Sweet - it works nicely! Cheers.
james6848