Display block by Taxonomy terms with a certain URL path
I want to display the block if the term is 21 (Chinese) and with "china" url path. I use this to control menu. If the term/tag is Chinese and the url path is china, the Chinese menu will show.
<?php
/*
This snippet returns TRUE if the node we are
currently viewing is tagged with specific terms,
or has a particular URL path assigned to it
*/
$desired_terms = array(21); // put here the term IDs (tid). can be more terms like (21,22)
$desired_path = 'china'; // put the URL path
// check taxonomy terms first
if ( arg(0) == 'node' and is_numeric(arg(1)) ) {
// Yes, we're viewing a node.
$node = node_load(arg(1));
foreach ($node->taxonomy as $term) {
if (in_array($term->tid, $desired_terms)) {
return TRUE;
}
}
}
// check url path next
// this should get the current drupal path, regardless of the clean url setting
if ($_GET['q']) {
$my_drupal_path = $_GET['q'];
} else {
$my_drupal_path = substr($_SERVER['REQUEST_URI'], 1);
}
// this will convert a path like node/37 to clean/url/path, if one exists
$my_path_alias = drupal_get_path_alias($my_drupal_path);
// check for the the url path component anywhere in the alias
// change this to $mypathalias == $desired_path to get an exact match instead
if (stristr($my_path_alias, $desired_path)) {
return TRUE;
}
// if all else fails, return false
return FALSE;
?>
See more: http://drupal.org/node/136029
The Opposite: Hide block in certain terms and certain url path
I want to hide block if the term is 21 (Chinese) and with "china" url path. I use this to hide Chinese menu (tag with Chinese, or url path is china).
<?php
$desired_terms = array(21); // put here the term IDs (tid) you're interested in
$desired_path = 'china'; // put the URL path component of interest here
// check taxonomy terms first
if ( arg(0) == 'node' and is_numeric(arg(1)) ) {
// Yes, we're viewing a node.
$node = node_load(arg(1));
foreach ($node->taxonomy as $term) {
if (in_array($term->tid, $desired_terms)) {
return FALSE;
}
}
}
// check url path next
// this should get the current drupal path, regardless of the clean url setting
if ($_GET['q']) {
$my_drupal_path = $_GET['q'];
} else {
$my_drupal_path = substr($_SERVER['REQUEST_URI'], 1);
}
// this will convert a path like node/37 to clean/url/path, if one exists
$my_path_alias = drupal_get_path_alias($my_drupal_path);
// check for the the url path component anywhere in the alias
// change this to $mypathalias == $desired_path to get an exact match instead
if (stristr($my_path_alias, $desired_path)) {
return FALSE;
}
// if all else fails, return false
return TRUE;
?>
For how to display block by Taxomony terms, see,
http://thanhsiang.org/faqing/node/44