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.


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';
  • 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 '16 at 15:50

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