Drupal 6 Unique Body Classes


/ Published in: PHP
Save to your folder(s)

Automatically add a class to the body tag on each page. Remember to add


Copy this code and paste it in your HTML
  1. /**
  2.  * Override or insert PHPTemplate variables into the templates.
  3.  */
  4. function phptemplate_preprocess_page(&$vars) {
  5.  
  6. // Add unique classes for each page and website section
  7. $path = drupal_get_path_alias($_GET['q']);
  8.  
  9. // append the body classes array with the current path
  10. $body_classes[] = phptemplate_format_class('page-' . $path);
  11.  
  12. // set body_classes var with spaces in between each class
  13. $vars['body_classes'] = implode(' ', $body_classes);
  14. if (module_exists('path')) {
  15. $alias = drupal_get_path_alias(str_replace('/edit','',$_GET['q']));
  16. if ( $alias != $_GET['q'] && $alias == $_REQUEST['q'] ) {
  17. $suggestions = array();
  18. $template_filename = 'page';
  19. foreach (explode('/', $alias) as $path_part) {
  20. $template_filename = $template_filename . '-' . $path_part;
  21. $suggestions[] = $template_filename;
  22. }
  23. }
  24. $vars['template_files'] = $suggestions;
  25. }
  26. }
  27. //Helper Function for formatting css class names
  28. function phptemplate_format_class($string) {
  29. return strtolower(preg_replace('/[^a-zA-Z0-9_-]+/', '-', $string));
  30. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.