0

I want to write a new Joomla template. How can I obtain the number of parent elements in my menu?

So far i tried this:

$app = JFactory::getApplication();
$menu = $app->getMenu();
$menu_items = $menu->getItems('menutype', 'topmenu');
// var_dump($menu_items);
foreach($menu_items as $i)
{
    if($i->parent == 1)
    {
        $itemCounter++;
    }
}

but at the end i get the wring number :(

  • 1
    What have you tried so far? Please show us that you have made the effort to do some research and tried something of your own – Lodder Aug 8 '14 at 11:31
2

A more effective method to get all the menu items that are on the top level (level 1) and part of the menu called "mainmenu" (default menu):

// Get default menu - JMenu object
$menu = JFactory::getApplication()->getMenu();

//Gets menu items by attribute (level == 1 and menutype == mainmenu)
$items = $menu->getItems(["menutype","level"],["mainmenu",1]);

//Show all menu items
var_dump($items);
1

It's not very clear what you're asking and what you expect to get, but try changing if($i->parent == 1 to if($i->level).

This should give you the number of top-level menu items in your main menu:

<?php
$app = JFactory::getApplication();
$menu = $app->getMenu();
$menu_items = $menu->getItems('menutype','mainmenu', false);
$itemCounter = 0;
foreach($menu_items as $i)
{
    if($i->level == 1)
    {
        $itemCounter++;
    }
}
echo $itemCounter;
?>

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.