Zen like body classes on custom theme
samwich - January 14, 2009 - 15:15
I had been using the Zen theme and creating subthemes for my sites, however the last site was rather small, so I decided to go completely custom.
In the zen theme if I were on www.example.com/about the body classes would include "page-about", in my custom theme it only spits out "page-node" for each page of the site.
I looked at the zen template.php and I think I found the function that adds to the body classes, but when I copy paste over and change the theme name, the site turns blank (so I did something wrong)
Has anyone implemented this way of printing the url alias to a body class on a custom theme?
this is the code I tried to copy paste
// Classes for body element. Allows advanced theming based on context
// (home page, node of certain type, etc.)
$body_classes = array($vars['body_classes']);
if (!$vars['is_front']) {
// Add unique classes for each page and website section
$path = drupal_get_path_alias($_GET['q']);
list($section, ) = explode('/', $path, 2);
$body_classes[] = CUSTOMTHEME_id_safe('page-' . $path);
$body_classes[] = CUSTOMTHEME_id_safe('section-' . $section);
if (arg(0) == 'node') {
if (arg(1) == 'add') {
if ($section == 'node') {
array_pop($body_classes); // Remove 'section-node'
}
$body_classes[] = 'section-node-add'; // Add 'section-node-add'
}
elseif (is_numeric(arg(1)) && (arg(2) == 'edit' || arg(2) == 'delete')) {
if ($section == 'node') {
array_pop($body_classes); // Remove 'section-node'
}
$body_classes[] = 'section-node-' . arg(2); // Add 'section-node-edit' or 'section-node-delete'
}
}
}
$vars['body_classes'] = implode(' ', $body_classes); // Concatenate with spaces
function CUSTOMTHEME_id_safe($string) {
// Replace with dashes anything that isn't A-Z, numbers, dashes, or underscores.
$string = strtolower(preg_replace('/[^a-zA-Z0-9_-]+/', '-', $string));
// If the first character is not a-z, add 'n' in front.
if (!ctype_lower($string{0})) { // Don't use ctype_alpha since its locale aware.
$string = 'id' . $string;
}
return $string;
}
}
If anyone is having trouble
If anyone is having trouble with this as well, I found the answer here
http://drupal.org/node/352501
there is a link to a blog post where it describes the correct function to put into your template.php file.
http://www.samwirch.com