Question

How to get only level 0 menu in Magento 2?

<?php $columnsLimit = $block->getColumnsLimit() ?: 0; ?>
<?php $_menu = $block->getHtml('level-top', 'submenu', $columnsLimit) ?>

give all menu items including submenu. I want only level 0 menu. Please help me.

Was it helpful?

Solution

for step by step implementation refer this link, for github code

for only show level 0 categories your template look like

<?php
$categories = $this->getStoreCategories(true,false,true);
$categoryHelper = $this->getCategoryHelper();
?>
<ul>
<?php
foreach($categories as $category):
     if (!$category->getIsActive()) {
        continue;
     }
?>
<li><a href="<?php echo $categoryHelper->getCategoryUrl($category) ?>"><?php echo $category->getName() ?></a></li>
<?php   
endforeach;
?>
</ul>   
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top