Last updated April 8, 2008. Created by Mojah on June 14, 2006.
Edited by add1sun, Heine. Log in to edit this page.
Ever wanted to display a block that appears in nodes that fall under one vocabulary? Look no further.
Edit the variable $desired_vocab in the code below to your desired vocabulary id to display the block in. The vocabulary id can be found by clicking on vocabularies and placing your mouse over the edit link.
This code works with both taxonomy and category modules.
<?php
// This snippet returns TRUE if the node we are
// currently viewing is tagged with a term which belongs
// to the 'desired_vocab' and we are not in edit mode (arg(2)).
// put here the vocabulary ID you're interested in
$desired_vocab = 1;
if ( arg(0) == 'node' and is_numeric(arg(1)) and arg(2) == FALSE ) {
// Yes, we're viewing a node in view mode.
$node = node_load(arg(1)); // cached
// If the term does not exist we're done
if (is_array($node->taxonomy)) {
foreach ($node->taxonomy as $term) {
if ($term->vid == $desired_vocab) {
return TRUE;
}
}
}
}
return FALSE;
?>Block visibility by term within a same taxonomy
You can just change the vid to tid. Both fields are from the same table, term_data, where vid is the vocabulary ID and tid is the term ID.
So the code for that is:
<?php
// This snippet returns TRUE if the node we are
// currently viewing is tagged with a term which is
// the 'desired_term' and we are not in edit mode (arg(2)).
// put here the term ID you're interested in
$desired_term = 59;
if ( arg(0) == 'node' and is_numeric(arg(1)) and arg(2) == FALSE ) {
// Yes, we're viewing a node in view mode.
$node = node_load(arg(1)); // cached
// If the term does not exist we're done
if (is_array($node->taxonomy)) {
foreach ($node->taxonomy as $term) {
if ($term->tid == $desired_term) {
return TRUE;
}
}
}
}
return FALSE;
?>
Comments
See also
See also:
if you want to display a
if you want to display a block by Vocabulary ID in taxonomy listing, having one or more terms (ex. www.italia-ru.it/taxonomy/term/64+81+82+83+84+85+86+87+88+89+90+91+92), use this code:
<?php
$match = FALSE;
// put here the vocabulary ID you're interested in
$vid = 2;
if (arg(0) == 'taxonomy' && arg(1) == 'term') {
$str_tids = arg(2);
$terms = taxonomy_get_tree($vid);
foreach ( $terms as $term ) {
$items[] = $term->tid;
}
$terms = taxonomy_terms_parse_string($str_tids);
//browse each value
foreach ($terms['tids'] as $k => $v) {
if (in_array($v, $items)) {
$match = TRUE;
}
}
}
return $match;
?>
Shorter version for checking term ID
The code for checking a Term ID can be shorter:
<?php
// This snippet returns TRUE if the node we are
// currently viewing is tagged with a term which is
// the 'desired_term' and we are not in edit mode (arg(2)).
// put here the term ID you're interested in
$desired_term = 59;
if ( arg(0) == 'node' and is_numeric(arg(1)) and arg(2) == FALSE ) {
$node = node_load(arg(1));
return (bool) $node->[$desired_term];
}
?>
You don't need to return FALSE explicitly; if nothing is returned, the block will not show.
Should be $node->taxonomy[$desired_term]
Thanks for this, but the return value needs to by an item in the $node->taxonomy array.
This code allow block to show
This code allow block to show in
training
training/*
content type 'product'
and all terms with taxonomy id 3,6,7
<?php
if ( ($_GET['q'] == 'training') or (substr($_GET['q'],0,9 ) == 'training/')){
return true;
}else{
// valid node id in view mode
if ( arg(0) == 'node' AND is_numeric(arg(1)) AND arg(2) == FALSE ) {
$node = node_load(arg(1)); // cached
if ( ($node->type == 'product') ) {
return true;
}
}else{
$match = FALSE;
// put here the vocabulary ID you're interested in
$vids = array(3,6,7);
if (arg(0) == 'taxonomy' && arg(1) == 'term') {
foreach( $vids as $vk => $vid){
$str_tids = arg(2);
$terms = taxonomy_get_tree($vid);
foreach ( $terms as $term ) {
$items[] = $term->tid;
}
$terms = taxonomy_terms_parse_string($str_tids);
//browse each value
foreach ($terms['tids'] as $k => $v) {
if (in_array($v, $items)) {
$match = TRUE;
}
}
}
}
}
return $match;
}
return FALSE;
?>
=======================
www.eleganza.biz
www.ayham.alsuleman.info
Nevermind
Answered my own question..