1

I'm using PHP to creata dynamic page title based on the menu item using Sourcer and the following PHP:

<?php
$active = JFactory::getApplication()->getMenu()->getActive();
echo $active->title;
?>

It works, but I would like to restrict the menu to the third level menu. So, if you go to a lower menu say the fourth or fifth, you would still have the third level showing for the title.

What do I add to this or I'm I going to need two different modules for items in the third or greater level menu items?

1 Answer 1

1

This snippet of code should get you close to what you want. It is untested so supplied as is.

It gets the page title from the current/active menu and if it is deeper than level 3 it will step up through the menu items to retrieve the menu title at level 3 and assign it to the page title property.

<?php
$menuItems      = Factory::getApplication()->getMenu();
$currentMenu    = $menuItems->getActive();
$menuLevel      = $currentMenu->level;
$pageTitle      = $currentMenu->title;
$parentMenu     = $currentMenu;

//Step up the menu tree until you get to level 3 menu item values.
while ($menuLevel > 3)
{
    $menuId     = $parentMenu->parent;
    $parentMenu = $menuItems->$menuId;
    $pageTitle  = $parentMenu->title;
    $menuLevel  = $parentMenu->level;
}

echo $pageTitle;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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