2

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.

3
  • Need to change position of menu? Jul 24, 2018 at 12:53
  • yes, I need to set the first position for the Home menu item. Jul 24, 2018 at 13:00
  • With same way or any other way? Jul 24, 2018 at 13:12

1 Answer 1

3

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

10
  • Great. I did not think about this. One more question. I want to add Home at the first place and Contact Me at the last place. Is it possible with a single plugin? Jul 24, 2018 at 14:02
  • If not, I will still upvote your answer. Jul 24, 2018 at 14:03
  • Accept my answer if resolve current asked issue. Jul 24, 2018 at 14:25
  • I have upvoted and accepted.Now can you answer on my question which I asked in the comment? Jul 25, 2018 at 5:25
  • I have updated my question now. Jul 25, 2018 at 5:30

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.