I can get the title of the active menu using
$menu = &Jsite::getMenu();
$menuname = $menu->getActive()->title;
But how can I access it's parent title?
Joomla Stack Exchange is a question and answer site for Joomla! administrators, users, developers and designers. It only takes a minute to sign up.
Sign up to join this communityTry this code:
$menu = JFactory::getApplication()->getMenu();
$parent = $menu->getItem( $menu->getActive()->parent_id );
$parentname = $parent->title
If you need the link
$parentlink = JRoute::_( $parent->link . '&Itemid=' . $parent->id );
The method getActive return a object with the active menu properties including the parent_id. The JMenu class has a method getItem($id), where $id is a menu id. This method return the same type of object that getActive. So, you can do something like this:
$menu = JFactory::getApplication()->getMenu();
$parent_id = $menu->getActive()->parent_id;
$parent_title = $menu->getItem($parent_id)->title;
Since you can get the active menu, you can get the rest by getting the tree
. This is usefull to traverse the item list, not just the parent!
So that you understand better, I'm spliting the answer in parts:
1. Create a variable for the active menu (for simplicity and portability):
$menu = &JSite::getMenu();
$active = $menu->getActive();
2. Pick the previous/parent menu by getting the previous item in the tree:
$parent_item_id = $active->tree[count($active->tree) - 2];
NOTE: the tree
is an array, and starts with zero, that's why I am subtracting by 2.
3. Finally get the menu title:
$menu->getItem($parent_item_id)->title
Mind you that you also need to add some checks to see if there is an active menu, and/or other error checks.
$menu = &JSite::getMenu();
, because it is not a static function. The correct way is to use the code as specified by @me7hos.
Apr 22, 2014 at 19:13