Hi,
Can anyone tell me how I can show a block only when the page is displaying a node of particular conent type.

For example I might want to show a block showing the weather only when a node created with a user defined (i.e. created with CCK)city content type, is being displayed.

Thanks in advance.

Comments

dnewkerk’s picture

Found this here and tested (works fine):

<?php
  // Only show if $match is true
  $match = false;

  // Which node types
  $types = array('book', 'news', 'anothernodetype');

  // Match current node type with array of types
  if (arg(0) == 'node' && is_numeric(arg(1))) {
    $nid = arg(1);
    $node = node_load(array('nid' => $nid));
    $type = $node->type; 
    $match |= in_array($type, $types);
  }

  return $match;
?>

Paste the code into the "Page specific visibility settings" area on the Block's settings page. Set the option to "Show if the following PHP code returns TRUE". Then just edit the $types = array('book', 'news', 'anothernodetype'); line and you should be good to go.

-- David
davidnewkerk.com | absolutecross.com
View my Drupal lessons & guides

Nigeria’s picture

Thanks a lot.

I really think that this sort of functionality should be in core so that you can just select which content types you want just like the visibility in regards to pages

Ade Atobatele

Ade Atobatele