Most people expect web sites to be easy to navigate. If visitors can't find what they want quickly and easily, they will probably leave your site very quickly. Controlling where block show can make your site a lot easier to navigate. This post explains how you can customise different blocks to be shown or hidden based on:
This post builds on an easier post I wrote that looked at how to control block visibility based on node types. For this post, I'll be using my films directory as a case study. As with the previous post, I'm not going to go into how to create blocks - there are many tutorials out there that can help you do this.
I want these blocks to show up when:
I achieved this by using some PHP code in the block settings. Don't worry if you don't understand PHP, you can copy this code and modify it to suit you (it goes in site building > blocks then configure your block and paste the code into 'Page specific visibility settings' - you will need to set this to 'Show if the following PHP code returns TRUE (PHP-mode, experts only)' and make sure that you are editing in plain text mode).
Here's the code I used (don't forget you will have to change some of the variables to suit your site).
<?php $match = FALSE; // show block if the node type is film $nid = arg(1); $type = $node->type; $match = TRUE; } } // show block for certain pages foreach($show_on_pages as $show_on_page) { } // show block for taxonomy pages foreach($vids as $vid) { if (arg(0) == 'taxonomy' && arg(1) == 'term') { $str_tids = arg(2); $terms = taxonomy_get_tree($vid); foreach ( $terms as $term ) { $items[] = $term->tid; } } $terms = taxonomy_terms_parse_string($str_tids); //browse each value foreach ($terms['tids'] as $k => $v) { $match = TRUE; } } } return $match; ?>
Update: I've made a tool that will generate code for your site.
Change the word film to the node type for your page. You can have more than one node type by adding new elements into the array. For example, if I wanted the block to display on node type kittens as well as films, I would edit line for to read like this
$types = array('film' => 1, 'kittens' => 1);
List all of the pages that you want the block to display on. For example, if you wanted to display the block on pages called foo, bar, and node/7 the line would be like this
$show_on_pages = array('foo','bar','node/7');
Put the ID number of the taxonomy vocabulary into the brackets. You can find the taxonomy vocabulary by going to content management > taxonomy > list - when you hover over the list terms link the ID number will be shown in the link reference in your browser's status bar (e.g. on my site, film series has this link: http:// penguin.ox4.org/admin/content/taxonomy/3). If you needed to change the IDs 6 and 12, line 19 would look like this
$vids = array(6,12);
See this code in action on my films database.
Next, I'm trying to work out how to create a block contains all of the links (e.g. when I'm looking at the Terminator film, it will list all of the other films in the Terminator series in a block). I'll write a new updated post when I've worked out how to do that.