I'm not sure what I'm doing wrong here. The block which holds the category links in is referenced to as navigation.sections. I thought by directing the following arguments toward the container I would be able to create a new link under it. Any help is appreciated.
<referenceContainer name="navigation.sections">
<block class="Magento\Framework\View\Element\Html\Links" name="mylink">
<arguments>
<argument name="label" xsi:type="string">Mylink</argument>
<argument name="path" xsi:type="string">mypath</argument>
<argument name="css_class" xsi:type="string">mycss</argument>
</arguments>
</block>
</referenceContainer>
Best Answer
[EDIT]
Apparently, in latest versions of M2 this does not work anymore.
Thanks to Max for pointing this out.
For later version you need to add a plugin for
Magento\Theme\Block\Html\Topmenu
instead of an observer.Add this to
etc/frontend/di.xml
and create the plugin class file
[Namespace]/[Module]/Plugin/Block/Topmenu.php
[/EDIT]
Original answer:
You can add elements to the top menu using the event
page_block_html_topmenu_gethtml_before
.So you need to create a module with these files (all the files should be in
app/code/[Namespace]/[Module]
):etc/module.xml
- the module declaration fileregistration.php
- the registration fileetc/frontend/events.xml
- the events declaration fileObserver/Topmenu.php
- the actual observerNow run in the cli
php bin/magento setup:upgrade
to install the module and you are good to go.