I created a content type called people, where I create personal pages using that type. One of the fields is 'area'.

Then, I created a view of block called 'software people', where I list all the people who work in software area. I want to show this block only on the pages where the person's area is Software. How can I achieve this?



This goal can be achieved in views as well, but only with some PHP contextual filter code:

Just add contextual filter: Content: area (field_area)

Under: "When the filter value is NOT available" select:

Provide default value-> PHP Code

Enter PHP code:

$node = menu_get_object();
return isset($node->field_area['und'][0]['value']) ? 
  ($node->field_area['und'][0]['value']) :'';

Under: "When the filter value IS available or a default is provided" select:

Specify validation criteria->PHP Code

$area = $handler->argument;   // content of field area
return ($area == 'Software'); // validation of the area content

action to take if filter value does not validate -> hide view

That's it! Your block will show up only on the pages where the person's area is 'Software'


Simplest way is to conditionally output region in node.tpl.php But there is no $page in node.tpl.php. So you need to make it happen. In your template.php add function

mytheme_preprocess_node(&$variables) { 
  // List of all regions for the current theme
  foreach (system_region_list($GLOBALS['theme']) as $region_key => $region_name) {
    // Add content to $region variable
    if ($blocks = block_get_blocks_by_region($region_key)) {
      $variables['region'][$region_key] = $blocks;
    else {
      $variables['region'][$region_key] = array();

Then in node.tpl.php use:

print render($region['region_name']);

to output your region. Of course wrap it in if, case or any other logic you fancy.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.