How To Add Links To The Top Menu In Magento 2

Hello Everyone,

In this blog, we will learn about how to Add a Custom Link with Top Menu Text in Magento 2.

Sometimes you may need to add a custom link to the top navigation menu for customer information

Without wasting your time, let us guide you straight away. Follow the easy steps given below to Add a Link to the Top Menu in Magento 2.

STEPS FOR ADD LINK WITH TOP MENU IN MAGENTO 2

Step 1: Create events.xml file

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

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">

    <event name="page_block_html_topmenu_gethtml_before">

        <observer name="vendor_extension_observer" instance="Vendor\Extension\Observer\Topmenulink" />

    </event>

</config>

Step 2: Create Top Menu Link.php file

app/code/Vendor/Extension/Observer/Topmenulink.php

<?php

namespace Vendor\Extension\Observer;

use Magento\Framework\Event\Observer as EventObserver;

use Magento\Framework\Data\Tree\Node;

use Magento\Framework\Event\ObserverInterface;

class Topmenulink implements ObserverInterface

{

    public function execute(EventObserver $observer)

    {

        $menu = $observer->getMenu();

        $tree = $menu->getTree();

        $data = [

            'name'      => __('Register'),

            'id'        => 'R1',

            'url'       => 'register.html',

            'is_active' => 'true'

        ];

        $node = new Node($data, 'id', $tree, $menu);

        $menu->addChild($node);

        return $this;

    }

}

?>

Step 3: Finally run the below commands

$ php bin/magento cache:clean

$ php bin/magento cache:flush

Step 4: Output:


Final Thoughts:

So this was the easiest way which we have told you in this blog. This is how you can Add Link in Top Menu Content in Magento 2. Hope you liked the blog.

So quickly go to the comment box and tell me how you like this blog?

Stay tuned with us on our site to get new updates of Magento.

Thanks for reading and visiting our site.      

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

Leave a Reply

Your email address will not be published. Required fields are marked *