Display Blocks on certain pages

Events happening in the community are now at Drupal community events on www.drupal.org.
cithukyaw's picture

Hi Drupalers,

I have two blocks - "Recent Blog Posts" and "Popular Blog Posts".
I would like to hide these blocks in all pages except blog related pages.
How can I do that ?

Comments

Context or pathauto

misc's picture

Before I used pathauto to do this, I created paths to blogpost that contained the path 'blog/', and in the path settings for the visibility I used /blog/*. Now I usually use the module context instead in different ways to accomplish this.

/* Mikke Schirén, https://digitalist/ */

Context Module

davidwheelerphd's picture

I recommend Context Module, also.

Thanks for your advices. I

cithukyaw's picture

Thanks for your advices.

I created a function "mymodule_block_visibility" in my custom module.

<?php
function mymodule_block_visibility($type, $visibility = FALSE){
$match = $visibility;
 
$url = request_uri();
 
$types = array('og' => 1, 'blog' => 1);

if (
arg(0) == 'node' && is_numeric(arg(1))) {
       
$nid = arg(1);
    
$node = node_load(array('nid' => $nid));
     
$type = $node->type;
       
# check node type
    
if (isset($types[$type]) && $type == $module) {
          
$match |= TRUE;
        }
  }
 
   if(
$module == 'blog'){
     
# check url
       
$match |= strpos($url, '=blog') | strpos($url, '/blog');
  }
 
   if(
$module == 'og'){ 
      
# Check og node id
    
$group_node = og_set_group_context();
     
$match |= $group_node->nid;
    
# check url
       
$match |= strpos($url, '=group') | strpos($url, '/group');
       
$match |= strpos($url, '=og') | strpos($url, '/og');
  }

return
$match;
}
?>

And call it in "Page specific visibility settings" of block configuration by checking "Show if the following PHP code returns TRUE".

<?php
return module_exists('mymodule') ? mymodule_block_visibility('blog') : TRUE;
?>

I think it is similar to the block_node_visibility module, but that module didn't work for me.
I prefer coding !

With Regards,
Sithu