As a new Magento developer. Every day I face new problems the problem I faced today is creating my own mega menu and I find it very difficult. As I can use the extension but I don't like the idea of using the extension. So I decided to customise and create a mega menu. so I researched and read this thread but I couldn't find my answer besides a provided link is not working anymore. so I wanted to make this Present Magento Menu to this in my custom theme My wanted Magento Menu. So now in topmenu.php of Magento\Theme\Block\Html I am afraid to change anything if I broke something. Present Magento Menu Codeenter image description here how can I replace This left-side code style with This right-side code style in Topmenu.php I am not an expert in Magento so if I change something without knowing it could break anything. So if anyone expert could help me out to accomplish my goal. Thanks in advance.

1 Answer 1


With the help of CategoryFactory you can get a collection of your categories, like so:

    public function getChildCategories($categoryId)
        $category = $this->categoryFactory->create();
        $categoryCollection = $category->load($categoryId);

        //Get category collection
        return $categoryCollection->getCollection()
            ->addAttributeToFilter('include_in_menu', 1)
            ->addAttributeToSelect(['name', 'url'])
            ->setOrder('position', 'ASC')

In your template you can then loop through the collection and build your mega menu.

  • Thanks for your reply. Do I need to add this code alongside the existent Topmenu.php or create a new Topmenu.php?
    – mrshiam
    Commented Oct 27, 2021 at 7:16

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.