Add a body class for the current node's taxonomy

Public

Founded at http://stackoverflow.com/questions/4642561/how-to-add-a-body-class-for-t...

Get raw version
php
  1. function MYTHEME_preprocess_html(&$vars, $hook) {
  2. if (!$vars['is_front'] && arg(0) == 'node') {
  3. $node = node_load(arg(1));
  4. $results = field_view_field('node', $node, 'taxonomy_catalog', array('default'));
  5. foreach ($results as $key => $result) {
  6. if (is_numeric($key)) {
  7. // Call drupal_html_class to make safe for a css class (remove spaces, invalid characters)
  8. $vars['classes_array'][] = "taxonomy-" . strtolower(drupal_html_class( $result['#title']) );
  9. // Add taxonomy ID. This will allow targeting of the taxonomy class even if the title changes
  10. $vars['classes_array'][] = "taxonomy-id-" . $result['#options']['entity']->tid;
  11. }
  12. }
  13. }
  14. }