简体   繁体   中英

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?

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.

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.

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/*

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM