Show based on alias or term
Last modified: March 28, 2009 - 16:26
I wanted to display particular block for pages tagged with certain terms, or with a certain URL path. This snippet variation does the job for me.
<?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(1, 2, 4); // put here the term IDs (tid) you're interested in
$desired_path = 'trucks'; // 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 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;
?>Blocking Desired Path Example
With the same code and a few tweaks, you can block the desired path
$desired_path = 'lascasas'; // put the URL path component of interest her Check the path and change the truthvalue check from TRUE to FALSE:
if (stristr($my_path_alias, $desired_path)) {
return FALSE;
}Now you can put the block in for all nodes with the taxonomy term but you don't include a views page result with all nodes in this term
