Add content type to body class I want to add a body class for the content type of each node. Drupal already provides this, but it isn't always consistent between the Node view, the Node/Add view and the Node/Edit view. This makes theming difficult. 

Here is my code in template.php

function MYTHEME_preprocess_html(&amp;$variables) {

    // Add Content type name to classes
    $content_type = $variables['page']['content']['system_main']['type']['#value'];
    $content_type_class = 'content-type-'.drupal_html_class($content_type);
    $variables['classes_array'][] = $content_type_class;

}


It works but I get this error message:


  Notice: Undefined index: type in wintheme_preprocess_html()


On Stack Exchange, it says the best approach is to use Isset().

If I do this, the error message goes away, but so does the class.

How can I keep my class working without the error message.
The problem is you're trying to use the same system_main content type variable that's not always defined. Try this instead:

$node = menu_get_object();
$type = isset($node-&gt;type) ? $node-&gt;type : 'none';
$type = ($type == 'none' &amp;&amp; arg(0) == 'node' &amp;&amp; arg(1) == 'add') ? arg(2) : $type;
$content_type_class = 'content-type-'.$type;
$variables['classes_array'][] = $content_type_class;


That should give you a $classes_array var with either "content-type-TYPE" or "content-type-none" for pages that aren't associated with nodes.