Drupal 6 - Only show block on specific node type

There are times where you want to hide Drupal blocks from certain pages but occasionally you want to have them only appear on certain types of content. This can be done with a small block of php code, this example works on Drupal 6, and it has been working great for us on multiple sites.

Take a backup of your site before you start, inserting php code can break stuff.

  • Determine the machine name of your node type (content management -> content types -> machine name is under the "type" column.
  • Create a custom block or edit an existing block
  • Scroll down and look for the page specific visibility settings near the bottom
  • Select "Show if the following PHP code returns TRUE (PHP-mode, experts only)"
  • Paste the following code but replace zzzzzzzzzz with your node type.
  • Save and check that the block appears only on those specific content types.

    type == $nodeType) OR ($node->nid == 1) ) {
         return true;    }}return FALSE;?>

    Also note on line 3 of the code there is an "or" statement allowing you to specifically note another node where you want this block to appear (in this case node/1). This can be used to create exceptions where the block appears on other specific pages as well, this is useful for admin only nodes.