Posted by adpo on October 13, 2008 at 9:45pm
| Project: | Drupal core |
| Version: | 6.5 |
| Component: | block.module |
| Category: | support request |
| Priority: | critical |
| Assigned: | adpo |
| Status: | closed (fixed) |
Issue Summary
I`m using Drupal 6, and want show block for a specific term. ID = 4
I found something like this, but is not working.
Thank you for your help.
<?php
//code snippet for causing block to show based on a node's taxonomy term OR on a designated string in the url.
$make_block_visible = FALSE;
$term_id_to_trigger_show_block = 4; // replace '21' your term id.
// The following code will cause block to show on the designated term's built-in "view" page such as:
// example.com/taxonomy/term/21
if ((arg(0) == 'taxonomy') && (arg(1) == 'term') && (arg(2) == $term_id_to_trigger_show_block)) {
$make_block_visible = TRUE;
}
return $make_block_visible;
?>
Comments
#1
I tried with this code as well. (not working)
<?php
$match = FALSE;
// put here the vocabulary ID you're interested in
$vid = 4;
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;
?>
#2
Your code seems to work fine?? possibly the problem is in the code controlling the block?
#3
I tried the first block of code by the way - i like simpler solutions.
#4
Thank you for reply.
I`m just pasting this code into block visibility options:
Show block on specific pages:
Show if the following PHP code returns TRUE.
Where is the problem?
#5
I think you have probably forgotten to add
<?php
?>
#6
Hi bruceclothier,
Thank you for reply.
Which script have you been trying?
PS. I`m using Drupal 6.
#7
Drupal 6.4, using:
<?php
//code snippet for causing block to show based on a node's taxonomy term OR on a designated string in the url.
$make_block_visible = FALSE;
$term_id_to_trigger_show_block = 4; // replace '21' your term id.
// The following code will cause block to show on the designated term's built-in "view" page such as:
// example.com/taxonomy/term/21
if ((arg(0) == 'taxonomy') && (arg(1) == 'term') && (arg(2) == $term_id_to_trigger_show_block)) {
$make_block_visible = TRUE;
}
return $make_block_visible;
?>
under the 'Show if the following PHP code returns TRUE' option of the block. I know this might be dumb, but have you checked all the basics? That you've selected the correct radio, that you've put the php open and close tags around the code, etc? Its this stuff that I easily forget to check. Your code does work fine though.
Bruce
#8
I notice that when I type:
samplewebsite/taxonomy/term/1
I can see that block, but when I`m on certain page with term 1, the block is missing.
I check it on my second website.
PS. I attached print screen of my settings.
#9
Yes, now that is all that you have asked it to do with that code (the one I tested). If you want it to show on more pages - i.e. all pages that have the term, you will have to add a bit of code. Just writing off the top of my head now I'd say something like this:
<?php
// YOUR CODE TAKES CARE OF LISTING PAGE
$make_block_visible = FALSE;
$term_id_to_trigger_show_block = 1; // replace '21' your term id.
if ((arg(0) == 'taxonomy') && (arg(1) == 'term') && (arg(2) == $term_id_to_trigger_show_block)) {
$make_block_visible = TRUE;
}
// THIS TAKES CARE OF NODES
if (arg(0) == 'node' && arg(1) && is_numeric(arg(1))) {
$node = node_load(arg(1));
foreach ($node->taxonomy as $term) {
if ($term->tid == $term_id_to_trigger_show_block) $make_block_visible = TRUE;
}
}
return $make_block_visible;
?>
#10
Thank you. It is working. Can I buy you a beer?
#11
No worries mate. Glad I could help.
#12
--project followup subject--
Automatically closed -- issue fixed for two weeks with no activity.
#13
Automatically closed -- issue fixed for two weeks with no activity.
#14
This code works great!
Can it be done with some sort of preg match so that it can catch all terms containing a certain sequence of letters?
for instance, it would place the block on terms: aloe vera, aloevera, aloe vera juice
#15
I hacked the code below together from a range of different sources (none of it is my own, but I did not record where it came from: if you want to claim it, please let me know!). It meets my 4 requirements in D6.8:
1) Show block for node with specific term ID
2) Show block for node with ancestors of term ID
3) Show block for listing page of specific term ID (e.g. taxonomy/term/id)
4) Show block for listing page of ancestor of specific term ID (e.g. taxonomy/term/idOfAncestor)
<?php
// Vocabulary term ID for which to display the block:
$displayTermID =7;
//Return value
$make_block_visible = FALSE;
//Deal with current node and ancestory
if ( arg(0) == 'node' and is_numeric(arg(1)) and arg(2) == FALSE ) {
// Get all taxonomy terms for current node:
$currNodeTerms = taxonomy_node_get_terms(node_load(arg(1)));
// If there are no terms, fail-fast:
if (is_null($currNodeTerms) || 0 == count($currNodeTerms)) {
return FALSE;
}
// For each term of the current node, get all the ancestor terms:
foreach($currNodeTerms as $term) {
$ancestors = taxonomy_get_parents_all($term->tid);
// Check for each ancestor term whether it is the term we are looking for.
// If it is, return TRUE immediately:
if (!is_null($ancestors) && 0 < count($ancestors)) {
foreach($ancestors as $ancestor) {
if ($displayTermID == $ancestor->tid) {
$make_block_visible=TRUE;
}
}
}
}
}
// Deal with index page, such as taxonomy/term/21
if ((arg(0) == 'taxonomy') && (arg(1) == 'term') && (arg(2) == $displayTermID)) {
$make_block_visible = TRUE;
}
//What about ancestor index pages?
if ((arg(0) == 'taxonomy') && (arg(1) == 'term')) {
$ancestors = taxonomy_get_parents_all(arg(2));
if (!is_null($ancestors) && 0 < count($ancestors)) {
foreach($ancestors as $ancestor) {
if ($displayTermID == $ancestor->tid) {
$make_block_visible=TRUE;
}
}
}
}
// If we didn't find our term of interest, return FALSE:
return $make_block_visible;
?>
#16
is this kind of what the context module does?
#17
Hi! Anyone who can modify this to appear on multiple terms and check their children pages? Any prompt response from you will be very much appreciated.
#18
I'm pretty new to this(php and drupal) but here's some code I came up with to show menu blocks on certain sections of my store. I have a root term for each section and this code finds the top parent of the current term. If anyone more experienced can help me make it better any help would be much appreciated.
<?php
// this code goes in only one block
global $section; //define global for sitewide use, eg. I put it in my body class
if (is_numeric(arg(1)))
{
$arg = arg(1);
if(arg(0) == "node"){ // if it's a node page
$node = node_load($arg);
$term = $node->taxonomy;
foreach($term as $v){ //get node terms(room to optimize this)
$tid = $v->tid;
}
}elseif(arg(0) == "catalog"){ // if it's a catalog page
$tid = $arg;
}
if($tid){
$t_parents = taxonomy_get_parents_all($tid, $key = 'tid'); // get parents
if($t_parents){
foreach($t_parents as $k=>$v){ //create array of all parent terms
$terms[$k] = $v->tid;
}
$section = array_pop($terms); // grabs the last term in the array which is the top parent.
}
}
}
// each block additional block only needs these 2 lines
global $section;
if ($section == 226){return TRUE; }else{ return FALSE;}
?>
#19
Yes. Was having trouble with this problem. The Context Module solved most of the issues without PHP. Excellent module.
http://drupal.org/project/context