Get the current menu level for in Drupal 7


As part of a big Drupal 7 development I needed the ability to find out the level a node (page) is on a menu tree, Drupal doesn't associate a node in menu trees by default so it can't be queried from the page or node variables.

The below function will identify the menu position based on the active trail flag in the given menu, just add the function to your template.php file and include the machine name of the menu that you want to query as the first parameter. the function will return an integer of the menu level.

function getCurrentMenuLevel($menu_name = null) 
     if(is_null($menu_name)) return null;

     $tree = menu_tree_page_data($menu_name);
     $level = 0;

     while ($tree) {
          while ($item = array_shift($tree)) {
               if ($item['link']['in_active_trail']) {
               if (!empty($item['below'])) {
                    $tree = $item['below'];
                    break 2;
     return $level;

This function can be really handy if you want to show a different layouts for different menu levels without having to create a different content type for each. Checkout the below example loop that can be used in your node template to show the data differently for different menu levels:

$level = getCurrentMenuLevel('menu-name');

if($level == 1) {
     //layout for level 1 item
} else {
     //layout for other levels