简体   繁体   中英

Joomla 3 get parent title of active menu item

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?

This should work

$menu = JFactory::getApplication()->getMenu();
$parent = $menu->getItem( $menu->getActive()->parent_id );
$parentname = $parent->title;
$parentlink = JRoute::_( $parent->link . '&Itemid=' . $parent->id );

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;

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM