Community Documentation

Show based on alias or term

Last updated March 28, 2009. Created by crbassett on April 13, 2007.
Edited by bekasu, add1sun. Log in to edit this page.

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

Comments

Thanks for the code. I use

Thanks for the code. I use this code to control my menu (block) to display the term "Chinese" and the url path with "china"
See here: http://ibc.ac.th/faqing/china

To hide the menu (Chinese) and the "china" url in English article, I use this code:

<?php

$desired_terms
= array(21); // 21 is the term for Chinese, replace it with yours
$desired_path = 'china'; // put the URL path component of interest here, mine one is china

// 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;
?>

Page status

No known problems

Log in to edit this page

About this page

Drupal version
Drupal 4.7.x, Drupal 6.x

Reference

Drupal’s online documentation is © 2000-2012 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License.
nobody click here