Search This Blog

Loading...

Monday, January 2, 2012

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 falsereturn FALSE;?>

0 comments:

Post a Comment

LinkWithin

Related Posts with Thumbnails