Take the 2-minute tour ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

This question already has an answer here:

I would like to be able to add an unpublished class to the body tag of a node (for both its main view and edit view) if the node has been unpublished.

How can I do this?


Steps I have taken:

1) Node Object The node object contains if a node has been published or unpublished (with $node -> Status), but I am not sure how to test this for each node and then add it to the body class.

This question is similar, but the provided answer doesn't work.

2) Drupal 6 This page explains how to do it for Drupal 6, but the code doesn't work for Drupal 7.

share|improve this question

marked as duplicate by MPD Mar 2 at 21:02

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1 Answer 1

up vote 5 down vote accepted

in your theme's template.php:

function mytemplate_preprocess_html(&$vars) {
  $node = menu_get_object();
  if ($node && isset($node->nid)) {
    if ($node->status) {
      $vars['classes_array'][] = 'published';
    } else {
      $vars['classes_array'][] = 'unpublished';
    }
  }
}
share|improve this answer

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