Posted by jmolivas on October 5, 2011 at 7:37pm
I am trying to add the taxonomy term-tid on current node as class to the body element using the preprocess-html.inc with no luck any help will be appreciated.
As explained on README.txt I added the preprocess-html.inc file also the code inside a function named MYTHEMENAMEHERE_alpha_preprocess_html
I already got the term-id also the code for adding new element to $variables['body_classes'][] = drupal_html_class( "taxonomy-term-". $taxonomy_term );
I can see the value if echo array $variables['body_classes'] but no class shown on body attribute when page rendered.
Best Regards
Comments
What version
What version of Drupal and Omega theme are you working with?
versions are
-Drupal 7.x
-Omega 3.x
--
http://jmolivas.com/
@jmolivas
template_preprocess_html
I recently had to add custom body classes for a site in Drupal 7 but I was adding classes based on a theme variable. I did it with template_preprocess_html.
http://api.drupal.org/api/drupal/includes--theme.inc/function/template_p...
Here's what I did. Maybe it will help you out.
function THEMENAME_preprocess_html(&$variables) {
if (theme_get_setting('my_variable') == 0) {
$variables['classes_array'][] = 'dogs';
}
elseif (theme_get_setting('my_variable') == 1) {
$variables['classes_array'][] = 'cats';
}
}
The code was put in the template.php of my theme.
Each theme is different
Each theme handles body classes differently. Even Omega for D6 handles it differently than Omega for D7.
The D7 version handles the declaration as follows:
<?php
$variables['attributes_array']['class'][] = drupal_html_class('taxonomy-term-' . $taxonomy_term);
?>
If you add that to your inc file as follows, it should work:
<?php
function MYTHEMENAMEHERE_alpha_preprocess_html(&$variables) {
// your code to get $taxonomy_term would go here and I'd probably recommend checking to see
// $taxonomy_term exists before adding the class so you don't end up with 'taxonomy-term-' as your body class
$variables['attributes_array']['class'][] = drupal_html_class('taxonomy-term-' . $taxonomy_term);
}
?>
Hope that helps.
Retrieving Taxonomy term How-To?
Hey,
I have a website where each node is tagged with taxonomy terms of a particular vocabulary. I need to style every node based on the term it was tagged with, and to do this I need to give every node a CSS class based on the taxonomy. The code you posted seems to add the retrieved taxonomy term name to the class array, the trouble I have is that I have no idea how I can retrieve the taxonomy term name and thus add it to the body, may you please help me with this?
How can I retrieve the taxonomy term on the node?
Thanks
Works perfect
Works perfect @gclicon
Actually I was solved with this workaround:
preprocess-html.inc
<?php
$variables['termclasses'] = drupal_html_class( "taxonomy-term-". $taxonomy_term );
?>
html.tpl.php
<?php
if (isset($variables['termclasses'])) { $attributes = substr($attributes,0,-1) .' '. $variables['termclasses'].'"'; }
?>
But I was not so proud of that code so I keep looking for the best way of solving it
So much thanks @gclicon & @jonathanmd for replying
Best Regards
--
http://jmolivas.com/
@jmolivas
alpha/omega_preprocess hooks use $variables differently
It appears that because of all the complexity Omega is managing, rather than it using the standard
$variables['classes_array']
directly (as template_preprocess_html does),omega/alpha_preprocess
hooks (in Omega'spreprocess-*.inc
files) first use$variables['attributes_array']['class']
to store its body classes.This confused me at first, but apparently is by design to help Omega to organize all its preprocess magic.
A note somewhere in Omega's preprocess README might help... I'll look for or create an issue.
Steve Oliver
This really helped me.. Thank
This really helped me.. Thank you! :)
I was struggling to use the
I was struggling to use the Omega theme in Drupal 6. The "no-sidebars" showing up in every body, whether or not sidebars were they was getting really annoying. I had add a hook in the template.php to add a "sidebar" class to the body to use in my css, of course, the no-sidebars is still there, but w/e, this works for now, I just would like for this theme to work properly in the future when it decides whether or not it should put the sidebar classes in the body.
Here was the code I added to my template.php:
<?php
function MYTHEMENAMEHERE_preprocess_page(&$vars, $hook) {
if ($vars['sidebar_first'] != "") {
$vars['body_classes'] .= " sidebar";
}
}
?>
Drupal 7 - Omega
To do this in Drupal 7 / Omega 7.x:
add to your template.php
<?php
function MYTHEMENAMEHERE_preprocess_html(&$variables) {
$variables['attributes_array']['class'][] = 'anewclass' ;
}
?>
.
.
$vars not $variables
A bit of an old thread, but the issue is you can not see the function call when you have the file preprocess-html.inc open. The function references $vars and not $variables, so you have to do something like:
if ( arg(0) == 'taxonomy' && arg(1) == 'term' ) {
$term = taxonomy_term_load(arg(2));
$vars['theme_hook_suggestions'][] = 'page__vocabulary__' . $term->vid;
}
My objective was to override the template for a vocabulary above.