Ok, basically I'm attempting to have blocks only show up on certain nodes; I can accomplish that at the moment by specifing the specific nodes; but the problem is; I have to keep updating the list everytime I add new content. I wanted to know is their anyway I can associate blocks with terms so that the block will only show if the node is categorized under the term.

Comments

mi5ha’s picture

Yes there is … if you know PHP a bit :-)

In “Page specific visibility settings” for block, you should choose “Show if the following PHP code returns TRUE (PHP-mode, experts only).”

And enter following code:

// show block only for these term ids
$term_ids = array(4, 56, 86);

if (arg(0)=='node' && intval(arg(1))) {
  $all_terms = taxonomy_node_get_terms(arg(1));
  $node_terms = array();
  foreach ($all_terms as $ik=>$iv) {
    $node_terms[] = $iv->tid;
  }
  $show = count(array_intersect($term_ids, $node_terms));
  return $show;
}

In $term_ids array you should enter the ids for which block should be visible. I coded this script pretty fast but I think it will do what you want. It will show the block only on node pages and only if node belongs to one of the terms from the $term_ids array.

Hope I helped

Misha
www.byteout.com

You can recognize truth by
its beauty and simplicity.
When you get it right, it is
obvious that it is right.

Richard Feynman, Scientist

bgt’s picture

I don't have the option of "if this php code is true" under "Page specific configuration settings". I only have the following two radio buttons; "Show on every page except the listed pages" and "Show on only the listed pages".

mi5ha’s picture

Oh, it seems that it’s a Drupal 4.7 feature, I guess you’re still on 4.6.

Nevertheless you could still use my snippet, look here for pointers: http://drupal.org/node/47916

Mamouri’s picture

I used of the following code to display a block only on forum nodes:

<?php 
global $node; 
return $node->type == 'forum';
?>

But it dosen't worked. Are you have any idea?

Mamouri’s picture

I myself found the solution. Maybe others are interested I put the code here:

<?php
if (arg(0) == 'node') {
  $node = node_load(array('nid' => arg(1)));
  return $node->type == 'forum';
}
?>

Cheers

Fluence’s picture

I'm trying to have the block also print on the front page. Can any of you tell me what might work? I've tried the following code:

<?php
if ($is_front) { return TRUE; }
elseif (arg(0) == 'node') {
  $node = node_load(array('nid' => arg(1)));
  return $node->type == 'page';
}
?>

This still works for my 'page' nodes, though.

Thanks in advance.