Hello friends, I am going to share my best megento practice to with you guys through this post . How To Add Custom Menu Link in Magento 2 Frontend And I hope that through this post you will get a lot of help in customizing the megento ecomm platform.
Here My Vendor_Name : Mageget
Here My Module_Name : CategoryAttribute
1) Create a di.xml file in - \app\code\Mageget\CategoryAttribute\etc\di.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Theme\Block\Html\Topmenu">
<plugin name="add_registry_inmenu" type="Mageget\CategoryAttribute\Plugin\Custommenu" sortOrder="10" disabled="false"/>
</type>
</config>
4) Create a file in Plugin folder - \app\code\Mageget\CategoryAttribute\Plugin\Custommenu.php
<?php
namespace Mageget\CategoryAttribute\Plugin;
class Custommenu
{
public function afterGetHtml(\Magento\Theme\Block\Html\Topmenu $menu, $html)
{
$giftTopUrl = $menu->getUrl('giftr/search/result'); /* Here is the menu link which are redirect after click */
$baseUrl = $menu->getUrl('*/*/*', ['_current' => true, '_use_rewrite' => true]);
if (strpos($baseUrl,'giftr/search/result') !== false) {
$html .= "<li class=\"level0 nav-5 active level-top parent ui-menu-item\">";
} else {
$html .= "<li class=\"level0 nav-4 level-top parent ui-menu-item\">";
}
$html .= "<a href=\"" . $giftTopUrl . "\" class=\"level-top ui-corner-all\"><span>" . __("Gift Registry") . "</span></a>";
$html .= "</li>";
return $html;
}
}