I am on Drupal 7. On the body tag, I need to create a new class that will detect the current page's menu depth. The class could carry the ID of the menu, then something that would detect the current page's menu depth. It could be something like: "primary-depth-0" for the top menu items, then "primary-depth-1", "primary-depth-2" and so forth... Is it easy to code that on template.php? I assume I have to add a new item into the $classes array...

Thank you.


1 Answer 1


I did something similar in the past, but I can't remember on which project, so I don't have working code.

Inside a template_preprocess_html, you get the breadcrumb via menu_get_active_breadcrumb. You then walk the breadcrump keeping track of depth, comparing the paths against the current path. When you find a match, you end the walk and then

$variables['classes_array'][] = "menu-depth-" . $depth;

I am pretty sure this is what I did.

  • Thank you! This works. Actually, I think $depth = sizeof($breadcrumb) will give me the depth number I need where $breadcrumb = menu_get_active_breadcrumb()... Is this right? I am using this and it works for me. May 2, 2012 at 4:53
  • If it works, then it is right. Couldn't quite remember exactly what menu_get_active_breadcrumb() returned at the $breadcrumb.
    – mpdonadio
    May 2, 2012 at 10:42
  • It appears it returns an array with all the elements of the breadcrumb array. May 2, 2012 at 21:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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