Question

I have added CMS page link to the top menu by following the below tutorial:

http://www.amitbera.com/magento2-add-a-cms-page-link-to-menu/

Since one of the custom menu items would be Home, so it should be the first item in the menu.

I want to know that how can I make some custom menu item as the first item in the top menu using the code in the tutorial.

Update: Suppose, I have 2 custom menu items, say Home and Contact. I want to make Home as the first menu item and Contact as the last menu item. Is it possible with a single plugin class? or I have to write 2 different plugins to achieve this goal?

Please help.

Was it helpful?

Solution

There is a issue with plugin order

In default Magento already plugin define in vendor/magento/module-catalog/etc/di.xml

<type name="Magento\Theme\Block\Html\Topmenu">
    <plugin name="catalogTopmenu" type="Magento\Catalog\Plugin\Block\Topmenu" />
</type>

And we created custom plugin in app\code{VendorName}{ModuleName}\etc\di.xml

<type name="Magento\Theme\Block\Html\Topmenu">
    <plugin name="add_cms_menu" type="{VendorName}{ModuleName}\Plugin\Topmenu" sortOrder="0" />
</type>

That means core and custom plugin, both are same exist with same sort order.

If you want to display custom menu just before category menu, sort order required minimum values to core value.

Use below code for plugin and check you custom menu

<type name="Magento\Theme\Block\Html\Topmenu">
    <plugin name="add_cms_menu" type="{VendorName}{ModuleName}\Plugin\Topmenu" sortOrder="-1" />
</type>

I hope it will be useful for you. Let me know if not working

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top