csm232s
csm232s

Reputation: 1660

Drupal 7: Display different Sidebar depending on Content Type

So I have three different blocks inside my right sidebar region. What I would like to do is change which one gets displayed based on the content type. I'm new to Drupal, and from what I can see in the available variables list, there's no way for me to see what content type I'm in when customizing block.tpl.php. Is it possible?

Upvotes: 2

Views: 4203

Answers (3)

Coder1
Coder1

Reputation: 13321

If you want to use just 1 block, you could add some code to get the node type:

<?php
if (arg(0) == 'node') {
  $node = node_load(arg(1));
}
?>

And now you can access the type with: $node->type

If you were going with 3 separate blocks ...

Install the path auto (http://drupal.org/project/pathauto)

Set your node paths to be different. For this example, I'll use articles and products as the content types:

Articles: articles/[title-raw] Products: products/[title-raw]

Then, set your blocks to display based on url path.

For your article block, use: articles/* For your products block, use: products/*

Upvotes: 0

Chance G
Chance G

Reputation: 171

There is no need for an extra module in Drupal 7. When you are editing the block (in the CMS, not the template file) look at the "Visibility settings" under "Content Types"

Here you can specify which types to show the block on.

Note: if none are selected then it will be available in every content type.

Upvotes: 0

Matt V.
Matt V.

Reputation: 9809

I'd recommend using the Context module to define different "contexts" for the different content types. Each context can then be configured to display different blocks.

Another alternative is the Panels module, but Panels does a lot more and consequently has a more complex interface, so it can be difficult for beginners to master.

Upvotes: 1

Related Questions