3

Is there a way to add body classes programmatically through template preprocessing without the use of a Javascript or a drupal module?

If so how would one do that? p.s. Using omega theme.

E.g.: I need to add a class to the body to change a particular background color.

0

2 Answers 2

9

You can add your class to body element by adding THEMENAME_preprocess_html() to your template.php Like :

function MY_THEME_NAME_preprocess_html(&$variables) {
  // Compile a list of classes that are going to be applied to the body element.
  // This allows advanced theming based on context (home page, node of certain type, etc.).
  $variables["classes_array"][] = "blue-bg";
  $variables["attributes_array"]["class"][] = "blue-bg";// For Omega based themes
}

Refer this link for more details.

1
  • 6
    On the off-chance the OP is using an Omega based theme that would be $variables['attributes_array']['class'][] = 'blue-bg';
    – Clive
    Nov 21, 2012 at 18:20
2

In the preprocess_html function in your template_php, you can add an item to the classes array which will render out the classes on your body. comme ca:

/**
 * Implements template_preprocess_html().
 */
function YOURTHEMENAME_preprocess_html(&$variables) {

  $variables['classes_array'][] = 'new-body-class';

}

This works on adaptive themes, and bartik based themes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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