{"id":2857,"date":"2016-05-08T23:22:40","date_gmt":"2016-05-08T17:52:40","guid":{"rendered":"http:\/\/www.magewallet.com\/?p=270"},"modified":"2020-03-13T16:07:54","modified_gmt":"2020-03-13T10:37:54","slug":"add-non-category-links-navigation-menu-magento-2","status":"publish","type":"post","link":"https:\/\/www.hiddentechies.com\/blog\/magento-2\/add-non-category-links-navigation-menu-magento-2\/","title":{"rendered":"Add a non-category links to the navigation menu in Magento 2"},"content":{"rendered":"<p>As we know Magento 2 does not include features like adding extra(home, blog) links to main navigation menu. In this blog post we will discuss how to add non category links to navigation menu in Magento 2. In this we will use Magento 2 Observer page_block_html_topmenu_gethtml_before to add new link to menu.<\/p>\n<p>Lets consider module with Namespace is Hiddentechies and Module Name is NavLinks. Lets list down all the files require for this extension.<\/p>\n<p>&#8211; registration.php<br \/>\n&#8211; composer.json<br \/>\n&#8211; etc\/module.xml<br \/>\n&#8211; etc\/frontend\/events.xml<br \/>\n&#8211; Observer\/Topmenu.php<\/p>\n<p>First we will create registration file at app\/code\/Hiddentechies\/NavLinks\/registration.php file<\/p>\n<pre class=\"lang:default decode:true \">&lt;?php\r\n\\Magento\\Framework\\Component\\ComponentRegistrar::register(\r\n    \\Magento\\Framework\\Component\\ComponentRegistrar::MODULE,\r\n    'Hiddentechies_NavLinks',\r\n    __DIR__\r\n);<\/pre>\n<p>Now, create a composer.json file in app\/code\/Hiddentechies\/NavLinks\/ folder as per below.<\/p>\n<pre class=\"lang:default decode:true \">&lt;?php\r\n\\Magento\\Framework\\Component\\ComponentRegistrar::register(\r\n    \\Magento\\Framework\\Component\\ComponentRegistrar::MODULE,\r\n    'Hiddentechies_NavLinks',\r\n    __DIR__\r\n);\r\n\r\n{\r\n    \"name\": \"hiddentechies\/navlinks\",\r\n    \"description\": \"N\/A\",\r\n    \"require\": {\r\n        \"php\": \"~5.5.0|~5.6.0|~7.0.0\"\r\n    },\r\n    \"type\": \"magento2-module\",\r\n    \"version\": \"1.0.2\",\r\n    \"authors\": [\r\n        {\r\n            \"name\": \"Hiddentechies\",\r\n            \"email\": \"admin@Hiddentechies.com\",\r\n            \"homepage\": \"https:\/\/www.Hiddentechies.com\/\",\r\n            \"role\": \"Developer\"\r\n        }\r\n    ],\r\n    \"autoload\": {\r\n        \"files\": [\r\n            \"registration.php\"\r\n        ],\r\n        \"psr-4\": {\r\n            \"Hiddentechies\\\\NavLinks\\\\\": \"\"\r\n        }\r\n    }\r\n}<\/pre>\n<p>now create module.xml file in app\/code\/Hiddentechies\/NavLinks\/etc<\/p>\n<pre class=\"lang:default decode:true \">&lt;?xml version=\"1.0\"?&gt;\r\n \r\n&lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"..\/..\/..\/..\/..\/lib\/internal\/Magento\/Framework\/Module\/etc\/module.xsd\"&gt;\r\n    &lt;module name=\"Hiddentechies_NavLinks\" setup_version=\"1.0.0\"&gt;\r\n    &lt;\/module&gt;\r\n&lt;\/config&gt;<\/pre>\n<p>now create events.xml file in app\/code\/Hiddentechies\/NavLinks\/etc\/frontend<\/p>\n<pre class=\"lang:default decode:true \">&lt;?xml version=\"1.0\"?&gt;\r\n&lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"urn:magento:framework:Event\/etc\/events.xsd\"&gt;\r\n    &lt;event name=\"page_block_html_topmenu_gethtml_before\"&gt;\r\n        &lt;observer name=\"hiddentechies_navlinks_observer\" instance=\"Hiddentechies\\NavLinks\\Observer\\Topmenu\" \/&gt;\r\n    &lt;\/event&gt;\r\n&lt;\/config&gt;<\/pre>\n<p>Now we will create Observer\/Topmenu.php file.<\/p>\n<pre class=\"lang:default decode:true \">&lt;?php\r\nnamespace Hiddentechies\\NavLinks\\Observer;\r\nuse Magento\\Framework\\Event\\Observer as EventObserver;\r\nuse Magento\\Framework\\Data\\Tree\\Node;\r\nuse Magento\\Framework\\Event\\ObserverInterface;\r\nclass Topmenu implements ObserverInterface\r\n{\r\n    public function __construct(\r\n        ...\/\/add dependencies here if needed\r\n    )\r\n    {\r\n    ...\r\n    }\r\n    \/**\r\n     * @param EventObserver $observer\r\n     * @return $this\r\n     *\/\r\n    public function execute(EventObserver $observer)\r\n    {\r\n        \/** @var \\Magento\\Framework\\Data\\Tree\\Node $menu *\/\r\n        $menu = $observer-&gt;getMenu();\r\n        $tree = $menu-&gt;getTree();\r\n        $data = [\r\n            'name'      =&gt; __('Menu Lable'),\r\n            'id'        =&gt; 'unique-id',\r\n            'url'       =&gt; 'Menu URL',\r\n            'is_active' =&gt; (expression to determine if menu item is selected or not)\r\n        ];\r\n        $node = new Node($data, 'id', $tree, $menu);\r\n        $menu-&gt;addChild($node);\r\n        return $this;\r\n    }\r\n}<\/pre>\n<p>Now run upgrade command <strong>php bin\/magento setup:upgrade<\/strong> to install the module. <strong>Clear Magento 2 cache<\/strong>.<\/p>\n<p>You have known all the steps to add new links to navigation menu of Magento 2. if you have any questions about this post, please ask them in comments.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>As we know Magento 2 does not include features like adding extra(home, blog) links to main navigation menu. In this blog post we will discuss how to add non category links to navigation menu in Magento 2. In this we will use Magento 2 Observer page_block_html_topmenu_gethtml_before to add new link to menu. Lets consider module&#8230; <\/p>\n<div class=\"actions\"><a href=\"https:\/\/www.hiddentechies.com\/blog\/magento-2\/add-non-category-links-navigation-menu-magento-2\/\">Continue Reading<\/a><\/div>\n","protected":false},"author":1,"featured_media":4885,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[59],"tags":[10,38,206,45],"_links":{"self":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/2857"}],"collection":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/comments?post=2857"}],"version-history":[{"count":3,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/2857\/revisions"}],"predecessor-version":[{"id":10511,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/2857\/revisions\/10511"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/media\/4885"}],"wp:attachment":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/media?parent=2857"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/categories?post=2857"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/tags?post=2857"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}