简体   繁体   中英

Get 2 Parent level Joomla Title PHP

I know how to get the Current Page Title , also I know how to get the Menu Active Page Title but I need to actually get the 2 parent Active Menu via PHP.

Active Menu Title

$app = JFactory::getApplication();
$menu = $app->getMenu();
$title = $menu->getActive()->title;

Menu Example

  • 1st Level - CARS
    • 2nd Level - FERRARI
      • 3rd Level - F50

If I am at the 3 level menu F50 how can I get the CARS active menu title?

Here you go:

$app = JFactory::getApplication();
$menu = $app->getMenu();
$baseId = $menu->getActive($params)->tree[0]; //tree[1] for the second parent etc 

//query the database to find the title
$db = JFactory::getDBO();
$query = "SELECT title FROM #__menu WHERE id=".$baseId;
$db->setQuery( $query );
$topParentTitle = $db->loadResult();

A method, i used this:

//import to faster development
jimport( 'joomla.application.categories' );

$options    = array();
$categories = JCategories::getInstance('Content', $options);
$category   = $categories->get($this->category->id);

$parent     = $category->getParent();

And here is the magic

//here you get the grandparent category
$grandtemp = $categories->get($parent->id);
$grandparent = $grandtemp->getParent();

//print the values of parent of parent (1st level)
print_r($grandparent)

if you have any question you can comment

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