Support Center

Adding custom body classes to the standard Drupal output based on the existence of some regions

Last Updated: Sep 05, 2013 05:49PM EEST
Here’s an example on how to add custom classes to the body tag output of your Drupal theme. Specifically, this example guides you on how to add a body class attribute when custom regions are not displayed.

​Just place the following code in your template.php file inside your theme’s folder.

Drupal 6

function YOURTHEMENAME_preprocess_page(&$variables) {
if (empty($variables['REGION'])) {
$body_classes = explode(' ', $variables['body_classes']);
$body_classes[] = 'no-REGION';
$variables['body_classes'] = implode(' ', $body_classes);

*Don’t forget to print $body_classes on your page template.

Drupal 7

function YOURTHEMENAME_preprocess_html(&$variables) {
if (empty($variables['page']['REGION'])) {
$variables['classes_array'][] = 'no-REGION';

If the preprocess function is already being called in your template.php file, then you will need to combine these code blocks together because you cannot call the same function twice.

Finally, clear all cached data. This step is important in order to force your template to be informed for the changes you have made in the template files. To do this go to Administer » Site configuration » Performance » Clear cached data

In case this article leaves any of your questions or concerns unanswered, please feel welcome to email us or post a public question.

​More than (just) Themes

Contact Us
seconds ago
a minute ago
minutes ago
an hour ago
hours ago
a day ago
days ago
Invalid characters found