Last updated February 15, 2011. Created by jbrauer on February 2, 2007.
Edited by tax14, ronald_istos, bekasu, domesticat. Log in to edit this page.
The following snippets allow you to configure a block to be shown or hidden depending on node id (nid) or node type. You can modify the arrays in the snippet to match your needs. Just modify and paste the appropriate snippet in the Block Visibility section when configuring a block.
How to use these snippets
- Go to your blocks administration section: "Home » Administer » Site building".
- Click 'Add block' to add a new block, or click 'Configure' on an existing block.
- Under "Page specific visibility settings" select "Show if the following PHP code returns TRUE (PHP-mode, experts only)."
- Paste the snippet in the text area just below. (NOTE: Remember to keep the <?php> tags.)
Modifying Arrays:
Node Type Array
$types = array('page' => 1);
Node ID Array
$nodes = array(1, 2);
Show a block based on $nid
The following will show the configured block in:
Example: http://www.example.com/node/1
Example: http://www.example.com/node/2
NOTE: For Drupal 7, replace $node = node_load(array('nid' => $nid)); with $node = node_load($nid) ; in the example code shown on this page.
<?php
// Only show block from types array and nodes array
$match = FALSE;
// Which node types
$types = array('page' => 1);
// Which nodes (by nid)
$nodes = array(1, 2);
if (arg(0) == 'node' && is_numeric(arg(1))) {
$nid = arg(1);
$node = node_load(array('nid' => $nid));
$type = $node->type;
if (isset($types[$type]) && in_array($nid, $nodes)) {
$match = TRUE;
}
}
return $match;
?>Show a block for only certain node types
<?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;
?>Hide blocks from certain node types
<?php
// Only show if $match is true
$match = true;
// Which node types to NOT show block
$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;
if(in_array($type, $types)) {$match=false;}
}
return $match;
?>Take different actions depending on node type
<?php
// Replace 'nodetype-x' with the node types you need
$nodetypes = array('nodetype-1', 'nodetype-2', 'nodetype-3');
if (in_array($node->type, $nodetypes)) {
// Begin your code for if the node's type was listed in the array
print $something;
// End code for if the node type was listed in the array
}
else {
// Begin your code for if the node type wasn't listed in the array
print $somethingelse;
// End code for if the node type wasn't listed in the array
}
?>
Comments
where's the "return" code
the fourth block doesnt return anything, how is it different than the upper three blocks?
Hiding a block on node edit pages
Using the code mentioned above i can show a block only on certain node types but the block is also visible on node edit pages. how can i hide it there?
The answer is...
I just found the solution:
<?php
// Only show if $match is true
$match = false;
// Which node types
$types = array('products' );
//getting the path
$url = request_uri();
$urlvalue = strpos($url, "edit");
// Match current node type with array of types and the path
if (arg(0) == 'node' && is_numeric(arg(1)) && $urlvalue === false ) {
$nid = arg(1);
$node = node_load(array('nid' => $nid));
$type = $node->type;
$match |= in_array($type, $types);
}
return $match;
?>
Node-level Blocks module
Maybe the Node-level Blocks module is a good solution for this.
http://drupal.org/project/node_level_blocks
You can assign blocks while editing a node.
panel types
Hi
I have the 2nd block down working well and is great but I have a slight issue in that this content type I created is manily used for sub menu content. So, the issue I have is allowing the block to be visible on panels or custom pages? Any ideas? I don't really wnat to use the areas in the panel as the theme block is far better.
Thanks
Andrew
Lifes like a box of drupal you never know what you'l get until you install one