Hide block for certain content type in Drupal 6

January 5, 2011
Quite often you want to hide or show a block only for a specific content type in Drupal. There is a quite simple hack you can do for achieving that! This is for Drupal 6, in Drupal 7 there is a core function that lets you do the same. Use this code to hide a block for a certain content type in drupal 6:
<?php
$match = TRUE;
$types = array(‘library’ => 1);
if (arg(0) == ‘node’ && is_numeric(arg(1))) {
$nid = arg(1);
$node = node_load(array(‘nid’ => $nid));
$type = $node->type;
if (isset($types[$type])) {
$match = FALSE;
}
}
if (drupal_is_front_page()) {
$match = FALSE;
}
return $match;
?>

When editing a block, paste this in the box on the bottom of the page and chose “show only if the following php-code returns ‘true’.” Then the block will show on every content type except the “library”-one, and neither on the frontpage. Of course you can change the “library” to whatever content type you want. As you see, a little lower in the code you find this: if (drupal_is_front_page()), that means that we hide it also on the frontpage. Remove that line and the two under it if you don’t want to use that function.

You like posts about Drupal? Then, read also my post about how to create quicktab blocks with cck fields inside.

I bet you like these posts as well:

  1. Create Quicktab blocks populated by cck fields
  2. Dynamic webproject, users produce the content
See more articles in the category: Drupal
  • Andreas

    Hi this doesn’t work? do you have an example of code where the block is only visible for content type X

    • http://twitter.com/JonathanBJ Jonathan Björkskog

      Hi! I think it works? just change the “library” to the content type you have. And if you change the first “$match = TRUE;” to “$match = FALSE;” and the second one the other way around (FALSE to TRUE), this code should work the other way (show the block only on the content type you chose, in this case “library”). Good luck! :)

Jonathan Björkskog, SEO and Facebook tricks doer

Important information

This is the search engine optimization (SEO) and web tactics webiste/blog for Jonathan Björkskog, working at and co-owning Genero Digital Agency, a digital advertising agency based in Helsinki, Finland. If you searched for my Swedish blog, you find it here: jonathanbjorkskog.net.

That's one of my passions.

If you were searching for the other one, kitesurfing, you should check out my kitesurfing blog instead. I am now as well surfing as a team-rider for Jesajasport, a kitesurfing shop in Finland selling Spleene kites, kiteboards and Stand Up Paddleboards.

Free SEO tool

Be sure to check your website's and blogs SEO-performance in our new SEO-tool! It is totally free!
Provided by Genero.fi
You want more hands-on help with writing optimized texts? Then check how to write search engine optimized texts here!

Hosted at Bluehost

Advertising

External links (in Swedish)

C’mon, I am on Facebook as well

Head over to the wall and send me all your questions about Facebook, SEO, or any other awesome topic on the internet!

This WordPress Template

Do you want this clean WordPress template that I am using on this blog? I will drop it as a FREE Premium theme on my template site when it gets ready. If you want it now, for testing and customizing, drop me a mail and I will send it your way. jonathan.bjorkskog[a]gmail.com, or ask for it on twitter..
Content Priority Theme by Jonathan Björkskog @ Genero