Block visibility php snippets

Block visibility php snippets

Categories:

Display a block on certain pages by url and by node type.

<?php

$match
= FALSE;
$types = array('content_type_one' => 1, 'content_type_two' => 2);
if (
arg(0) == 'node' && is_numeric(arg(1))) {
 
$nid = arg(1);
 
$node = node_load(array('nid' => $nid));
 
$type = $node->type;
  if (isset(
$types[$type])) {
   
$match = TRUE;
  }
}

$desired_paths = array('page1', 'page2');

if (
$_GET['q']) {
  
$my_drupal_path = $_GET['q'];
} else {
  
$my_drupal_path = substr($_SERVER['REQUEST_URI'], 1);
}
$my_path_alias = drupal_get_path_alias($my_drupal_path);
foreach (
$desired_paths as $desired_path) {
   if (
stristr($my_path_alias, $desired_path)) {
      
$match = TRUE;
   }
}

return
$match;

?>

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
1 + 3 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.