Tell me more ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

I would like to add class to the HTML element based on taxonomy. Is it possible to do it from inside template.tpl.php or do I need to create needed templates manually? If so, how will my theme know which template it should pick? Thank you

share|improve this question

1 Answer

up vote 1 down vote accepted

You can do this by adding a preprocess function to your template file that detects if the current node has terms associated with it and if so, populates a variable for the page.tpl.php according to your logic, ex:

function YOURTHEME_preprocess_page(&$variables) {
  if (!empty($variables['node'])) {
    $node = $variables['node'];
    if (!empty($node->field_tags)) {  

      #your taxonomy logic here, if term matches your case then assign it to variable
      $variables["custom_taxonomy_page_class"];  

    }
}

Then, in your page.tpl.php file in your theme, you just need to add the above variable where you want it in the HTML tag.

<html xmlns="http://www.w3.org/1999/xhtml" class="$custom_taxonomy_page_class">

If the variable is empty, no class will be output.

share|improve this answer
great. thank you very much! – loparr Apr 7 at 1:45

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.