Blocks that appear based on taxonomy in Drupal 6
Discovering the power of the Views and Taxonomy modules solved a huge number of frustrations I had when I was designing our site. But to get the subsequent Blocks to appear on the correct pages with the correct content is tricky.
This problem is easily solved with some PHP cut and paste magic that gets inserted in the configure section of the specific Block you want to control (See image for the exact location of where the code goes). This page in the Drupal forums has a list of great snippets that can help with organizing Blocks with PHP. I was stuck two of them together to get the following code, that makes a specific block appear on certain pages, and also a certain page type if the taxonomy term is correct.
To find out the number corresponding to any of your Taxonomy terms, go to the Taxonomy section in your admin menu and in "list terms", hover the mouse over any given term and the term's number will appear at the bottom of your browser.
To use this code, just replace the term number and the Drupal paths with your own and change the FALSE / TRUE variables as you like.
<?php
$match = FALSE;
$url = request_uri();
if (strpos($url, "fogalarmbuilding")) {
$match = FALSE;
}
if (strpos($url, "community_stories")) {
$match = FALSE;
}
if (strpos($url, "saturnina_stories")) {
$match = TRUE;
}
$desired_term = 4;
if ( arg(0) == 'node' and is_numeric(arg(1)) and arg(2) == FALSE ) {
$node = node_load(arg(1));
if (is_array($node->taxonomy)) {
foreach ($node->taxonomy as $term) {
if ($term->tid == $desired_term) {
$match= TRUE;
}
}
}
}
return $match;
?>
Drupal Taxonomy Blocks Views modules
This entry was posted
on Monday, April 20th, 2009 at 12:52 am and is filed under Saturna Project.
You can follow any responses to this entry through the RSS 2.0 feed.
You can leave a response, or trackback from your own site.