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.

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?

This is what I've been looking for and trying to do for weeks. One question though. If my nodes' terms are actually items of my primary menu, how can I get the vocabulary id of that menu? Is a the menu id number the same as a vocabulary id?

nevermind i think.

Nevermind. Got things working by creating a separate vocabulary from the primary menu. I realized I needed both a category for the nodes AND a primary menu mirroring those main section pages of the site. Great site, Nick! I've picked up a lot in the past couple weeks. Excellent place for not-quite-noob (but definitley not goo-roo) theme developers!

short remark

Just checking if I didnt over see something. Wouldnt it be more logical to use the term for the ID and the vocabulary for the class? But else big thk U for the code, just what I was looking for.

It could go either way. I

It could go either way. I suppose I just used vocab for the ID because I was think hierarchy.

never mind that last comment

never mind that last comment of mine about the cookie problem It's fixed :)

Hi, thanks for the code, but

Hi, thanks for the code, but I tried to implement it on my VbDrupal site, and then I got this upon logging out: Unable to add cookies, header already sent. File: /home/hlc/public_html/themes/clearwind/template.php Line: 21 ..It only brok the logout function on the drupal frontend (allthough user sessions are processed through vbulletin), and line 21 corresponds to where I put your code. Is there any way to prevent this error? JR

Hmmmm… I'm not sure what

Hmmmm... I'm not sure what considerations one might have to make with vbdrupal. Could you post your entire template.php file? I don't really have enough info.

You Rock!

I am learning so much from this site. Thank you.. :)

Rock!

I have to admit I have no

I have to admit I have no idea who they are, but they are a very scary looking bunch

Judas Priest, my friend. I

Judas Priest, my friend. I didn't pick them because I like their music. I actually think they are goofballs of the highest order. However, they were picked for soley for their humorous apperence, and their connection to the word "rock". Sometimes, when I explain why I had thought something was funny, life just isn't worth living...

The situation must be salvaged

Regardless of how scary Judas Priest does look (its a wonder to step back and look at how pop/rock culture has changed. Is it evolving or regressing? Tough one.) and how many times we have to explain the joke : Life is always worth living. See the character Robert Benini plays in La Vida es Bella for audio visual example. Great code. How can one modify it to control block visibility within a node based on $vid? If someone could kindly post here or http://drupal.org/node/69012 (here).

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd><p><h1><h2><h3><h4><h5><h6><code><cite><blockquote><img>
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • Lines and paragraphs break automatically.

More information about formatting options