Michael Angeles' post on creating different looks for different sections by switching body classes and ID's inspired me to write up a related method.
The advantages of this method are that it dynamically builds classes on the basis of vocabulary and term that allow you to have wildy different styles for every node.
This method requires two parts, one snippet in your template.php file, and a special body tag in your page.tpl.php file.
template.php
<?php
function _phptemplate_variables($hook, $vars) {
switch ($hook) {
case 'page':
/* Get the taxonomy info from the current node while in the page */
$node_taxonomy_object = $vars['node']->taxonomy;
/* make sure the taxonomy object exists to prevent errors */
if ($node_taxonomy_object) {
foreach($node_taxonomy_object as $term) {
/* only use selected vocabulary IDs to generate classes -- lists these below (1, and 21 are just examples) */
switch($term->vid) {
case 21:
case 1:
/* build the $body_class from the term's name, replacing spaces with "-" */
$vars['body_class'] = str_replace(" ","-",$term->name);
/* build the $body_ID from the term's vocabulary name, replacing spaces with "-" */
$vocabulary_array = taxonomy_get_vocabulary($term->vid);
$vars['body_id'] = str_replace(" ","-",$vocabulary_array->name);
break;
}
}
}
break;
}
return $vars;
}
?>
page.tpl.php
" class="<?php print $body_class;?>">
I trust anyone with a fair background can figure out the practical applications of this method from here on. Now, back to work for me.
Comments
How to deal with other content?
I've used this code on a couple of sites this month and it's one of my new favourites. One thing I'd like to understand is how I can specify the bodyIDs of some of the other parts of the site, such as the site-wide contact form and views. Is it something to do with setting up other cases?
<?php
switch ($hook) {
case 'page': /* rest of code */
case 'contact': /* hard code the bodyID */
case 'view01': /* hard code the bodyID for a particular view */
?>
Would it be similar to the above? And if so, how do I specify the contact form and the view?
Keep up the sterling work!
All the best,
Richard
Thanks for being there!
You help me so much!
And It's a pleasure to read your technical litteracy.
As a newbie, coming from
As a newbie, coming from wordpress I have implimented the above, yet I cannot move forward. What is coding to make this as part of the header.
I have a vocabulary named Departments and one of my terms is named Elder Charles Smith (with "Elders" being the parent to this term)
My Style Sheet looks like this:
/*HEADER STUFFS */
#Departments .Elders {
background: #000 url(images_test/elder_charles.jpg) top right no-repeat;
}
I tested the image to see if it would show up using the generic #Header id and it does. But when I try to impliment your method it does not show up. Where am I going wrong?
category module
Hi!
Thanks for this code. It might be just what i need. Does it work with the “category module”?
Do I need to write down all ID i want to work or can I have a sort of "all cases" thing?
Thanks again.
primary menu vid?
nevermind i think.
short remark
It could go either way. I
never mind that last comment
Hi, thanks for the code, but
Hmmmm… I'm not sure what
You Rock!
Rock!
I have to admit I have no
Judas Priest, my friend. I
The situation must be salvaged
Post new comment