Community Documentation

Determine what level of the menu hierarchy a page is on

Last updated August 26, 2009. Created by Owen Barton on April 2, 2009.
Edited by bekasu. Log in to edit this page.

Description

Often, you need to know if a page (node, view or otherwise) has a menu item, and if so, what "level" of the menu it is on (level 1 being the top level menus, level 2 being the sub-menus, level 3 being the sub-sub menus etc). Once you determine this, you can style pages differently depending on their menu level.

<?php
 
// Fetch the menu tree for the current page.
 
$tree = menu_tree_page_data('primary-links');
 
$level = 0;
 
// Go down the active trail as far as possible.
 
while ($tree) {
   
// Loop through the current level's items until we find one that is in trail.
   
while ($item = array_shift($tree)) {
      if (
$item['link']['in_active_trail']) {
       
// If the item is in the active trail, we count a new level.
       
$level++;
        if (!empty(
$item['below'])) {
         
// If more items are available, we continue down the tree.
         
$tree = $item['below'];
          break;
        }
       
// If we are at the end of the tree, our work here is done.
       
break 2;
      }
    }
  }
 
// Then, add body classes or other theme variables as needed:
 
if ($level <= 2) {
   
$vars['body_classes'] .= ' landing';
  }
  if (
$level) {
   
$vars['body_classes'] .= ' menu-level-' . $level;
  }
?>

Notes

  • If you use another menu than "Primary Links" here, enter the menu name or ID instead of 'primary-links'.
  • The $vars['body_classes'] part assumes that this code is placed in your theme_preprocess_page() function in template.php, however the main logic should work pretty much anywhere.
  • This snippet will return 0 (zero) if the current page has no menu item in the requested menu - note that this can include the front page of the site. It is recommended that you use $vars['front_page'] (already generated) to specifically check for the front page.

Page status

No known problems

Log in to edit this page

About this page

Drupal version
Drupal 6.x
Audience
Designers/themers, Programmers

Theming Guide

Drupal’s online documentation is © 2000-2013 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License. Comments on documentation pages are used to improve content and then deleted.
nobody click here