Show block by taxonomy term - Drupal 5, 6

http://drupal.org/node/113651

<?php
  $myterm
= 21; // 21 is my site here for the term Chinese. You can replace this id with the term id you want to use.
  // This will show on all nodes having this term
 
if ((arg(0) == 'node') && is_numeric(arg(1))) {
   
$terms = taxonomy_node_get_terms(arg(1));
    foreach(
$terms as $term) {
      if (
$term->tid == $myterm) return TRUE;
    }
  }
 
// This will show on the index page for that term
 
if ((arg(0) == 'taxonomy') && (arg(1) == 'term') && (arg(2) == $myterm)) {
    return
TRUE;
  }
 
// Otherwise
 
return FALSE;
?>

To do the same for more than one taxonomy term use in_array() instead:

<?php
  $myterms
= array(8, 9, 12, 21); // list the ids of the terms you want
  // This will show on all nodes having this term
 
if ((arg(0) == 'node') && is_numeric(arg(1))) {
   
$terms = taxonomy_node_get_terms(arg(1));
    foreach(
$terms as $term) {
      if (
in_array($term->tid, $myterms)) return TRUE;
    }
  }
 
// This will show on the index page for that term
 
if ((arg(0) == 'taxonomy') && (arg(1) == 'term') && (in_array(arg(2), $myterms))) {
    return
TRUE;
  }
 
// Otherwise
 
return FALSE;
?>

Display block only for forums

<?php
if (arg(0) == 'forum') {
  return
TRUE;
}
if (
arg(0) == 'node' && ctype_digit(arg(1))) {
 
$node = node_load(arg(1));
  if (
$node->type == 'forum') {
    return
TRUE;
  }   
}
return
FALSE;
?>

Display block by Taxonomy terms and with a certain URL path

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

See here: http://thanhsiang.org/faqing/node/155

<?php  $myterm = 21; // 21 is

<?php
  $myterm
= 21; // 21 is my site here for the term Chinese. You can replace this id with the term id you want to use.
  // This will show on all nodes having this term
 
if (($node = menu_get_object()) && !empty($node->taxonomy[$myterm])) {
    return
true;
  }
  ..
 
// Otherwise
 
return FALSE;
?>

<?php
  $myterms
= array(8, 9, 12, 21); // list the ids of the terms you want
  // This will show on all nodes having this term
 
if ($node = menu_get_object()) {
    return
array_intersect(array_keys($node->taxonomy[$myterm], $myterms) ? TRUE : FALSE;
  }
  ....
 
// Otherwise
 
return FALSE;
?>

<?php
if (arg(0) == 'forum') {
  return
TRUE;
}
if (
$node = menu_get_object()) {
  return
$node->type == 'forum'
}
return
FALSE;
?>

However, most of this can be done via the context module

Finally a snippet to show only in the forum.

Thanks a million, spend ages on this with silly taxonomy block modules etc..

more specifically

I did some testing, and found that it's the "administer taxonomy" permission that has to be turned on for this to work.

display problem

When I use this code, the block only shows when the admin is logged in. What am I doing wrong?

Very helpful!

Thanks, this is exactly what I was looking for!

Awesome! Thanks!!!!

Awesome! Thanks!!!!