User login

Add a subsection class to Drupal page body for theming Views and other parts of a site that can be defined by parts of the path

I had wanted to add the name of the view with its display to the body classes (Views apparently provides everything but that combination, and not all browsers we need to support deal with matching on two sibling classes). Looked in Zen for how to do it first though and as a consequence saw a way to add new sub-section body class with the first two parts of each URL.

This goes in your zen subtheme's implementation of preprocess page:

<?php
  // Add unique class for each website sub-section.  Zen provides a section.
  $path_parts = explode('/', drupal_get_path_alias($_GET['q']), 3); 
  $path_parts_count = count($path_parts);
  if ($path_parts_count > 1) { // We have a sub-section.
    if ($path_parts_count > 2) {
      array_pop($path_parts);
    }
    $subsection = implode('-', $path_parts);
    $vars['body_classes_array'][] = zen_id_safe('subsection-' . $subsection);
  }
  $vars['body_classes'] = implode(' ', $vars['body_classes_array']); // Concatenate with spaces
?>

Searched words: 
drupal view page name in body class

Comments

thanks!

this helped. I was having trouble pulling workable parts from the path.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • You can use Markdown syntax to format and style the text. Also see Markdown Extra for tables, footnotes, and more.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <img> <blockquote> <small> <h2> <h3> <h4> <h5> <h6> <sub> <sup> <p> <br> <strike> <table> <tr> <td> <thead> <th> <tbody> <tt> <output>
  • Lines and paragraphs break automatically.

More information about formatting options

By submitting this form, you accept the Mollom privacy policy.