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


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


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.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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