I would like to make a block visible only when the node is a blog article ( or forum article etc ). I reason that some php code in the { admin | block | Page specific visibility settings } should do the work. I am a drupal and php newbie and do not know where to start. Could someone provide a code snippet for that? Thanks!

Comments

StuartMackenzie’s picture

I've always used pathauto module and clean urls so accomplish the task like so..

In the block visability options, select 'Show on only the listed pages.' and then in the box below
have

blog/*

the asterix acts as a wild card so the block will show on all pages where the path is http://yoursite.com/blog/...whatever...

there is probably some php code that does the same thing but being a newbie myself this is the easier approcah IMO.

Stuart Mackenzie
http://rojojam.com

nevets’s picture

First you need to determine if you are on a page displaying a single node then what the node type is

<?php
// Our we viewing a single node?
if ( arg(0) == 'node' && is_numeric(arg(1)) && !arg(2) ) {
  // Viewing a single node, get the node
  $node = node_load(arg(1));
  // Test for node type, we use a switch to make it easy to look for more than one node type
  switch ( $node->type ) {
  case 'blog':
  case 'page':
    // You can remove or add case statements
    // Always of the form "case 'content-type':"
    // You can look at "Administer" -> "Content Managment" -> "Content Types" to see the types
 
    // In this case we return TRUE (visible) if a blog entry or page
    return TRUE;
  }
}
// If we get here we don't want the block visible
return FALSE;
?>
markpape’s picture

Thanks for this. Just for the next person... include a "break;" at the end of each case for the code to work.

nevets’s picture

In the example code, there is no break on purpose. This allows us to handle multiple types in the same way, without the break, the earlier case statement 'falls into' the next case statement.

gladiator84’s picture

Thanks! I implemented this to only display a block on content type of 'product'. I did, however, need the break in the case statement after returning TRUE. For some reason my block would still display in places where I didn't want it to, even after the return statement. I tried clearing the cache, browser cache, etc.