Show blocks only on pages (nodes) of a certain type

Problem

I have a block that I want to appear in the sidebar, but only when the viewer is looking at a certain node type.

Update: I've written an newer version of this post that allows you to control block visibility by page and taxonomy vocabulary as well as node type.

Here's a case study. I have a node type call cute_pics. I have a sidebar that lists the last 10 pictures I've uploaded. I want the sidebar to show when a visitor is on a cute picture page. You can see the pictures on my page here.

Note: This post won't tell you how to create a block using the views module - there are plently of other Drupal tutorials to do that (e.g. learnbythedrop).

I've assumed that you've created the block and moved it to the relevant part of your page by going to Admin > Site building > Blocks.

Bad solution

I could go to the block that I want to show and configure it. Under the Page specific visibility settings on the configuration page I could check Show on only the listed pages (remembering to use the plain text editor), and then add each node as I create a new cute picture.

This is not a good solution since it means I have to remember to add each node to the list of the listed pages. If I craete a new cute picture and forget, the sidebar block will not appear on the page. This is very likely to happen if many people are contributing content to the site.

Good solution

I can use a little piece of PHP to say 'if the node is a cute picture, show the block.' To do this:

  • Go to Admin > Site building > Blocks.
  • Click the Configure link next to the relevant block.
  • Under the Page specific visibility settings select Show if the following PHP code returns TRUE (PHP-mode, experts only).
  • Make sure you're using the plain text editor.
  • Paste this code into the Pages section and click save (change the bit on line 3 where it says 'cute_pics' for your node type).

  1. <?php
  2. $showblock = FALSE;
  3. $types = array('cute_pics' => 1);
  4. if (arg(0) == 'node' && is_numeric(arg(1))) {
  5. $nid = arg(1);
  6. $node = node_load(array('nid' => $nid));
  7. $type = $node->type;
  8. if (isset($types[$type])) {
  9. $showblock = TRUE;
  10. }
  11. }
  12. return $showblock;
  13. ?>

  • Click save.

See this code in action on

penguin's cute pictures

(it's the 'previously on cute' block on the right of the page).