Web Analytics

Fill the form & Get Installation Support

Magento 2: Display Category Image In Navigation Menu

Let’s say I have to show category images to navigation menu.

Magento doesn’t take a use of any kind of template to draw the whole menu, but a function which will retrieve all the categories & create li/ul tree.

In Magento 2.1.X you can easily perform this task by rewriting the _getHtml() function from the class located at Magento\Theme\Block\Html\Topmenu. It’s kind of function which will call to itself again-n-again. We will make an observer which will add our programmed code in the navigation menu.

1. Create your module

Let’s start by creating one folder in app/code/Vendor/ModuleName.
Here, ‘vendor’ is the namespace for your modules.
app/code/Vendor/ModuleName/etc/module.xml

Now the module registration:

2. Rewrite this class Magento\Theme\Block\Html\TopMenu

Create a file called di.xml to declare the rewriting of the class where the _getHtml() method is going to get called:
app/code/Vendor/NavigationMenu/etc/di.xml

After this you are allowed to create the class which will allow to rewrite the method. Now the following points:

app/code/Vendor/NavigationMenu/Rewrite/Block/Html/Topmenu.php

3. Make observer which will triggered by the event

To perform it we have to create the events.xml configuration file and assign the class which will handle the event:

app/code/Vendor/NavigationMenu/etc/frontend/events.xml

app/code/Vendor/NavigationMenu/Observer/AddContentToCategoryTopmenu.php

 

Fill the below form if you have any Magento programming need.





    Recent Articles

    Author Info

    Author Image

    Magento Developer

    Hire S. Gorjia

    Get a Free Quote





      /* second popup */

      Connect With Us