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.

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.

  • 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 '12 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, privacy policy and cookie policy

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