7

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?

10

Try 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 );
  • While this answer is correct, you should check the return value of getActive() because there maybe is no active menu and you get a non-object back (I think null) – Harald Leithner Apr 22 '14 at 18:24
1

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;
1

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.

  • You shouldn't use $menu = &JSite::getMenu();, because it is not a static function. The correct way is to use the code as specified by @me7hos. – Don Gilbert Apr 22 '14 at 19:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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