Joomla 3 get parent title of active menu item
Asked Answered
S

3

5

I found on this thread : get active menu item title how to get the title of active menu item :

$menu = &Jsite::getMenu();
$menuname = $menu->getActive()->title;

but how can I get his parent menu item title?

Singspiel answered 28/8, 2013 at 21:18 Comment(0)
I
8

This should work

$menu = JFactory::getApplication()->getMenu();
$parent = $menu->getItem( $menu->getActive()->parent_id );
$parentname = $parent->title;
$parentlink = JRoute::_( $parent->link . '&Itemid=' . $parent->id );
Ish answered 10/11, 2013 at 19:12 Comment(2)
How can I get the parent menu link?Ralli
I needed this for a submenu title and had to add two more variables: $menulevel = $menu->getActive()->level; $activename= $menu->getActive()->title; Then I added the title like this: <?php echo ($menulevel == 1)?$activename:$parentname; ?>Bolo
O
2

It's been while since I worked with Joomla, nevertheless give this a try:

$menu     = JSite::getMenu();
$active   = $menu->getActive();
$parent   = $menu->getItem($active->parent);

Then you can use $parent as any other menu item:

echo $parent->title;
Oedema answered 28/8, 2013 at 21:31 Comment(2)
Just tried but it doesn't work, it doesn't retrieve anything. thx for tryingSingspiel
How can I get the parent menu link?Ralli
B
2

What I just used was this:

$menu = &JSite::getMenu();
$active = $menu->getActive();
$menuname = $active->title;
$parentId = $active->tree[0];
$parentName = $menu->getItem($parentId)->title;
$parentlink = $menu->getItem($parentId)->alias;
echo "<h1><a href=\"$parentlink\">".$parentName."</a></h1>";

Found most of this in the Joomla forum and added the alias part, which I guessed ... This works with SEF URLS and URL rewriting on. Anyway, the title line:

&JSite::getMenu()->getItem(&JSite::getMenu()->getActive()->tree[0])->title;

... which should be the same as what AlexP listed, I didn't check if it's exactly the same.

Bolo answered 6/7, 2014 at 13:54 Comment(1)
getMenu() isn't a static method. The code will work, but it'll give warnings. The correct way to do this in Joomla 3 without getting PHP warnings is as follows: $menu = JFactory::getApplication()->getMenu();Uganda

© 2022 - 2024 — McMap. All rights reserved.