1

I want add schema in my Joomla menu.

My code:

$menu = JFactory::getApplication()->getMenu();
// Get menu items - array with menu items
$items = $menu->getItems('menutype', 'mainmenu');
//print_r($items);
?>
<ul itemscope itemtype="http://www.schema.org/SiteNavigationElement">
    <?php foreach($items as $i => $menu_item){ ?>
        <li itemprop="name"><a itemprop="url" href="http://www.10leaves.ae/"><?php if($menu_item->level == '1') { echo $menu_item->title; } ?></a>
            <?php if($menu_item->level == '2') { ?>
            <ul>
                <li><?php echo $menu_item->title; ?></li>
            </ul>
            <?php } ?>
        </li>
    <?php } ?>
</ul>

The problem is, the sub-menu is not being displayed.

For Example :

<ul itemscope itemtype="http://www.schema.org/SiteNavigationElement">
    <li itemprop="name"><a itemprop="url" href="http://www.10leaves.ae/">Home</a></li>
    <li itemprop="name"><a itemprop="url" href="http://www.10leaves.ae/">DIFC</a></li>
    <li itemprop="name"><a itemprop="url" href="http://www.10leaves.ae/">ADGM</a></li>
    <li itemprop="name"><a itemprop="url" href="http://www.10leaves.ae/">About</a></li>
    <li itemprop="name"><a itemprop="url" href="http://www.10leaves.ae/">Publications</a></li>
    <li itemprop="name"><a itemprop="url" href="http://www.10leaves.ae/">FinTech: Now Everybody Wants A Slice of the Pie</a></li>
    <li itemprop="name"><a itemprop="url" href="http://www.10leaves.ae/">The Role of Financial Centres in Emerging Economies</a></li>
    <li itemprop="name"><a itemprop="url" href="http://www.10leaves.ae/">Bitcoin versus Ethereum </a></li>
    <li itemprop="name"><a itemprop="url" href="http://www.10leaves.ae/">ADGM</a></li>
    <li itemprop="name"><a itemprop="url" href="http://www.10leaves.ae/">DIFC</a></li>
    <li itemprop="name"><a itemprop="url" href="http://www.10leaves.ae/">Finance</a></li>
    <li itemprop="name"><a itemprop="url" href="http://www.10leaves.ae/">Contact</a></li>
</ul>

The Publications menu has submenus FinTech and two more.

I cannot figure out how to print the submenus.

0

Assuming that the parent menu precedes it child menus, I think it is most sensible to batch the submenus and then just before a new level = 1 item occurs, you unload the batch of submenus in your desired structure.

I should declare that I am new to Joomla and I do not know if there is a native Joomla way to perform this task. Also, I don't personally like bouncing in and out of php because I think it increases the chances of developer error, so I'll write this up as one continuous php block.

Code: (Demo)

echo '<ul itemscope itemtype="http://www.schema.org/SiteNavigationElement">';
    foreach ($items as $i => $menu_item) {
        if($menu_item->level == '1') {
            if ($i) {             // not the first iteration ($i is not 0)
                if ($sub_menu) {  // only display a batch of submenus if batch is not empty
                    echo '<ul><li>' , implode('</li><li>', $sub_menu) , '</li></ul>';
                }
                echo '</li>';
            }
            $sub_menu = array();  // start over with a new empty batch
            echo '<li itemprop="name">';
                echo '<a itemprop="url" href="http://www.10leaves.ae/">' , $menu_item->title , '</a>';
        } elseif ($menu_item->level == '2') {
            $sub_menu[] = $menu_item->title;  // add to the batch
        }
    }
    if ($items) {
        echo '</li>';             // close final listitem tag
    }
echo '</ul>';                     // close first unorderedlist tag

Output:

<ul itemscope itemtype="http://www.schema.org/SiteNavigationElement">
    <li itemprop="name">
        <a itemprop="url" href="http://www.10leaves.ae/">Home</a>
    </li>
    <li itemprop="name">
        <a itemprop="url" href="http://www.10leaves.ae/">DIFC</a>
    </li>
    <li itemprop="name">
        <a itemprop="url" href="http://www.10leaves.ae/">ADGM</a>
    </li>
    <li itemprop="name">
        <a itemprop="url" href="http://www.10leaves.ae/">About</a>
    </li>
    <li itemprop="name">
        <a itemprop="url" href="http://www.10leaves.ae/">Publications</a>
        <ul>
            <li>FinTech: Now Everybody Wants A Slice of the Pie</li>
            <li>The Role of Financial Centres in Emerging Economies</li>
            <li>Bitcoin versus Ethereum </li>
        </ul>
    </li>
    <li itemprop="name">
        <a itemprop="url" href="http://www.10leaves.ae/">ADGM</a>
    </li>
    <li itemprop="name">
        <a itemprop="url" href="http://www.10leaves.ae/">DIFC</a>
    </li>
    <li itemprop="name">
        <a itemprop="url" href="http://www.10leaves.ae/">Finance</a>
    </li>
    <li itemprop="name">
        <a itemprop="url" href="http://www.10leaves.ae/">Contact</a>
    </li>
</ul>

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.