If I have the mid of a menu item, how can I determine what level the menu item lives at in the menu hierarchy? Or can I simply determing if the menu item has a parent or is a top level item?

Thanks!

Comments

flamingvan’s picture

Can't be. There must be an easy way.

flamingvan’s picture

$parent = menu_get_item($vars['mid']);
$vars['parent'] = $parent['pid'];

$hasParent = menu_get_menu();
$hasParent = $hasParent['items'][$parent]['pid'] == 0 ? 0 : 1;

I hope the helps somebody!

akahn’s picture

Hey flamingvan, can you provide a little explanation of what your code is doing? When I put this in a node and print $parent and $hasParent, I get an error: warning: Illegal offset type in /Applications/MAMP/htdocs/drupal/includes/common.inc(1352) : eval()'d code on line 9.. I'm trying to print in each node a link to its parent node, if it has one. Thanks for any help.

nimzie’s picture

I'm trying to use this code as well and am unable to get it to work whether I print the vars out or not.

I'd like to use this with D 5.x and output the top level parent menu item.

Thanks for any help.

Cheers,

Adam

grasmash’s picture

Simplest way:

$active_trail = menu_get_active_trail();
end($active_trail);
$parent = prev($active_trail);

This will work in your template.php file.

bbinkovitz’s picture

This only works if you're looking for the parent item of a menu link that exists in the current page active trail, no? If I have a mlid that I want to look up the parent for, this won't work. Is that correct?

c_note’s picture

Extremely useful snippet of code, thanks