Building Dynamic CSS Classes With Taxonomy
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.