I want to make it so certain blocks only appear on specific content types (for example: "page") is there an easy way to do this with a module? I've been searching so far the only solution I see is using php code in the block, I've been playing around with the views module to see if it can do this but it seems it can't. Any suggestions are appreciated

Comments

nevets’s picture

No module that I know of, people generally use PHP to control the visibility of blocks based on content type.

CompShack’s picture

I use this in my code

 if (in_array($node->type, array('blog', 'othertype',)))
      print custom-block; // only want custom blog on "blog" and "othertype"

I tried Site building › Blocks and then "Show if the following PHP code returns TRUE (PHP-mode, experts only)." option - I entered the following:

//only print block if we are viewing a blog entry
if in_array($node->type, array('blog')

But it doesn't seem to work. Anyone know why?

-----------------------------------------
Finally, I CMS that I Like!
http://www.CompShack.com

nevets’s picture

$node is not defined in a block, you need something like

if ( arg(0) == 'node' && is_numeric(arg(1)) ) {
  $node = node_load(arg(1));
  if (in_array($node->type, array('blog', 'othertype'))) {
      print custom-block; // only want custom blog on "blog" and "othertype" 
  }
}
CompShack’s picture

nevets - your code was missing the "return true" for the block to display :) - here is a working code - tested and works great.

//The code will display a block when a user is viewing a blog entry.
if ( arg(0) == 'node' && is_numeric(arg(1)) ) {
  $node = node_load(arg(1));
  if (in_array($node->type, array('blog'))) {
 return TRUE; 
  }
}

-----------------------------------------
Finally, I CMS that I Like!
http://www.CompShack.com

nevets’s picture

See what happens when you copy and paste and only answer the question asked :)

abu3abdalla’s picture

good job

mohd nashaat
abu3abdalla@gmail.com
mohdnashaat@msn.com
eng_abu3abdalla@skype
www.tajahdev.com