How can I hide blocks at specific pages in Drupal 7

I would like to hide some blocks on specific pages on my Drupal 7 website. How can I do this?

Skywalker's picture

There are many possibilities

If you scroll down in the block setting page then you will find the visibility settings. There you will find many differnt options on how to determine the visibility of the block. In the first option "Pages" you have again 3 options. If you check "All pages ecept those listed" then you can enter site pathes which should be excluded from viewing the block. Thats pretty simple. Just follow the instruction below. The secound checkbox allows you to specify "Only the listed pages". Thats pretty selve explaining.

Sometimes it is a little bit more difficult. Maybe you have special content site which have a file path containing only the title as example. In this cas the mentioned methods will not work. Thats why the third option "Pages on which this PHP code returns TRUE (experts only)" takes palce. There you can specify by php code whether to show the block or not. Here  I will give a code expample:

 

<?php
// Only show if $match is true
$match = false;
$types = array('question', 'blog');
if(drupal_is_front_page()){
  $match = true;
}elseif(arg(0) == 'node' && is_numeric(arg(1))){
  $nid = arg(1);
  $node = node_load($nid);
  $type = $node->type;
  $match |= in_array($type, $types) && !(strpos($_SERVER['REQUEST_URI'], '/edit') !== FALSE);
}elseif(arg(0) == 'blog' && empty(arg(1))){
  $match = true;
}
return $match;
?>

The code gives a little bit more control. You can insert the code and check what happens. It hides the block for example on edit views. 

 

But the visibility setting can do more than this. You can show the block only for specific roles or only on specific content types. You can also let people decide if they wish to hide it or not. The visibility settings are very flexible. You can set up whatever you want. 

Vote the answer: 
0
No votes yet