Drupal Answers is a question and answer site for Drupal developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 '14 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.

up vote 6 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
    
This will work for EACH theme you add it to, so if you are using a backend theme you have to add it to the template.php file for that theme as well as your front-end theme. – DrCord May 20 at 15:50

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