Closed (fixed)
Project:
Zen
Version:
6.x-1.x-dev
Component:
layout.css
Priority:
Normal
Category:
Feature request
Assigned:
Unassigned
Reporter:
Created:
23 Apr 2008 at 18:02 UTC
Updated:
28 May 2013 at 06:41 UTC
Hello...
I just thought I'd post this as I've already found myself this change for two sites. Basically I had the need to render some special CSS for nodes with a specific taxonomy term assigned.
So I'm just overriding phptemplate_preprocess_node() and adding:
if (isset($vars['taxonomy'])) {
foreach($vars['taxonomy'] as $term_info) {
$node_classes[] = 'taxonomy-'.zen_id_safe($term_info['title']);
}
}
After the portion where node-type classes are set. I think this really could be useful for many situations where you want a node teaser to change in some way based on an assigned term.
Just a thought.. and thanks to everyone for work on Zen, it's excellent.. :)
Comments
Comment #1
Moonshine commentedActually I ended up needing to expand on this code a bit, so I'll post my preprocess hooks here.
Basically it sets taxonomy based class additions to the "body" for a node "page", and taxonomy based classes to individual divs for node teasers. That way they can be targeted differently via CSS. I can certainly roll it into a more direct patch for Zen if there is interest in adding it... it's quite simple.
_preprocess_page:
_preprocess_node:
Comment #2
johnalbinWouldn't using the taxonomy ID be safer than using the name? That way you could, for example, fix a typo in the name without changing your CSS.
Plus, what if you are using Chinese characters for your taxonomy name? zen_id_safe would convert it to an empty string.
Comment #3
Moonshine commentedGood points. I actually started with the id, but ended up changing to tag names as it just allowed for a few things that I liked:
But your certainly correct, especially re: other languages.
Personally I think having both ID and name (if not blank) would be the best of both worlds though. I'll add that to the code, as I can see where ids would definitely come in handy for situations were #2 above is not the desired action.
Updated idea:
_preprocess_page:
_preprocess_node:
Comment #4
naught101 commentedThis is a great idea. It would also make sense to have the node-id and the node-name in the body class as well.
Comment #5
naught101 commentedComment #6
artis commentedDRUPAL 5.x Comment:
This works in Drupal 5 Zen Theme also!
I added part of this code in my template.php file under the function _phptemplate_variables line 173, right before the break; in the case 'node': section
This works beautifully because you get the name and id classes in the node div. I don't really see a need to have them in the body element so I skipped that part.
Comment #7
wilfn commentedUsing Zen 6.x-1.0: this only worked for me when I substituted "zen_preprocess_node" in place of "phptemplate_preprocess_node" and "$classes" in place of "$node_classes" (in Moonshine's updated version above). Also taking out the test for 'teaser' content and the _preprocessor_page (I only need the node classes), the code snippet now looks like this:
Hope this is of help to someone. Comments appreciated also if I've done something wrong (I'm a bit of a newbie).
Oh, and I heartily agree with Moonshine's original comment - thanks to all responsible for Zen!
Comment #8
eatanicecream commentedNone of these seem to work for me, all I get is
<body class="not-front logged-in page-node node-type-book two-sidebars">I'm working with a site that wasn't originally built by me, so I've no idea what theme it was based on (I'm presuming it was ZEN). I don't know much PHP either...
I'd like to get body_classes to display the taxonomy terms for each node.
Here's my template.php file:
Many thanks!
Comment #9
naught101 commentedShould never have changed that, since there is no patch. Sorry.
Comment #10
echoz commented@naught101, you had changed that status 5 yrs ago! Since the last post was from 3yrs ago and not sure it was Zen, I'm marking this as fixed.