Hey folks. I'm trying to change a few things on my site using CSS based on the section being viewed. I am sectioning off my site using taxonomy. I've used the taxonomy theme module in the past, but because I only want to change a couple CSS values, that's very inefficient and difficult to deal with.
After looking for a good solution, I discovered that it's fairly easy to give the body tag a class based upon which taxonomy is being viewed (I grabbed this code snippit from http://drupal.org/node/67587 ). The following code placed in the template.php file accomplishes this basic task:
<?php
function _phptemplate_variables($hook, $vars) {
if ($hook == 'page') {
$prefix = ' b-tax-';
}
if ($hook == 'node') {
$prefix = ' tax-';
}
if (module_exists('taxonomy') && $vars['node']->nid) {
foreach (taxonomy_node_get_terms($vars['node']->nid) as $term) {
$vars['node_terms'] = $vars['node_terms'] . $prefix . eregi_replace('[^a-z0-9]', '-', $term->name);
}
}
return $vars;
}
?>
All I need to do then is add class="<?php print $node_terms; ?>" to the body tag and I have a body class based on taxonomy. Awesome, right?
So here's what I need help with. I know 0 PHP and I'd like to extend this code a bit to include checking for the front page as well as having it hook into pages made with views.
My front page consists of no direct content and instead I use blocks generated by views to display only a few teasers from each taxonomy group.
If anyone could help me out with this, I'd love them forever :)
Thanks!
Comments
From the Zen theme: function
From the Zen theme:
and in page.tpl.php
Hope that works!
Oh my gosh, thanks!
Oh my gosh, thanks! I think I'll just go with the Zen theme, actually. It seems like it's made for this type of thing moreso than Bluemarine.
Thanks dude, you rock!
Doesn't work in the latest Zen theme
This the exact type of function i need, but alas, it doesn't work in Zen Theme 6.x-1.0-beta3
Can somebody help me?
Look at the handbooks
Look at the handbooks regarding theming Drupal 6. The _phptemplate_variables function has been replaced with multiple preprocess functions.
Ok, so
This is the original zen (6.x-1.0-beta3) preprocess page function with the same code I copied from the first post.
I need the taxonomy terms to be listed in the body class=""
it's the foreach thing that
it's the foreach thing that doesn't seem to be working...
Ok... figured it out. People
Ok... figured it out.
People who want to use taxonomy terms listed in the zen theme,
put this code into your template.php file.
thanks
thanks that works great!
In Drupal 6
In Drupal 6 taxonomy_node_get_terms no longer takes a nid argument, it expects the node object instead. The quickest way to fix is to load the node object and then pass that to taxonomy_node_get_terms.
E.g.
$node = node_load($vars['node']->nid);
foreach (taxonomy_node_get_terms($node) as $term) {
$vars['node_terms'] = $vars['node_terms'] . 'b-tax-' . eregi_replace('[^a-z0-9]', '-', $term->name);
}
Add as snippet?
Hi there,
I'm trying to do the same thing as MichaelJohnston. I have different sections on my website which need to be themed differently. I was looking for an easy solution (my PHP is fairly basic) and they all seem fairly complex. However I have found two approaches that work for me and are easy to implement and don't entail more than 10 lines of code.
The first is to use the taxonomy approach and add a taxonomy term to the body class. This works well except for views. The other approach is to use part of the URL - I use Pathauto where each section has a different prefix for the URL which can then be extracted and added to the body class.
With the help of drupal_jon above, my code for the taxonomy which I put into template.php was:
In taxonomy_node_get_terms_by_vocabulary($node,1), "1" is the vocabulary I'm using to denote the section style.
Using the URL approach, the code I used is:
This gets the first section after the main URL.
For both methods I then updated the body class line in page.tpl.php to
I thought it might be helpful to other people to add these as snippets? As my PHP is basic if anyone has a minute to see if I have missed anything, otherwise I could add them.
For Zen 6.x-2.0
In Zen 6.x-2.0 I had it working by modifying the above body class line in page.tpl.php to
<body class="<?php print $classes . $body_class; ?>">I think that it should
I think that it should be: