I need to add Custom Link In Main Menu Only apply Specific Text. currently i have display menu as category. i want to add link only where to buy menu. please help me & Suggest how can i add menu?
Thanks In Advance
I need to add Custom Link In Main Menu Only apply Specific Text. currently i have display menu as category. i want to add link only where to buy menu. please help me & Suggest how can i add menu?
Thanks In Advance
If the last 4 links are custom links, then you can those custom links add this using Plugin.
See: http://devdocs.magento.com/guides/v2.3/extension-dev-guide/plugins.html
Create a plugin on class Magento\Theme\Block\Html\Topmenu as the menu is rendered from this class and use before plugin on getHtml().
The menu is rendered from this class and use before plugin on getHtml().
1. create a module.module should have minimum below files:
app/code/{VendorName}/{ModuleName}/etc/module.xml
app/code/{VendorName}/{ModuleName}/composer.json
app/code/{VendorName}/{ModuleName}/registration.php
2: create di.xml where we will define the plugin.
location: app\code{VendorName}{ModuleName}\etc\frontend\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_cms_menu" type="{VendorName}{ModuleName}\Plugin\Topmenu" sortOrder="20" />
</type>
</config>
3. Now create the plugin class where we will add a cms page link to the menu.
public function __construct(
NodeFactory $nodeFactory,
\Magento\Cms\Model\PageFactory $pageFactory,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Framework\UrlInterface $urlBuilder
) {
$this->nodeFactory = $nodeFactory;
$this->_pageFactory = $pageFactory;
$this->_storeManager = $storeManager;
$this->_urlBuilder = $urlBuilder;
}
public function beforeGetHtml(
\Magento\Theme\Block\Html\Topmenu $subject,
$outermostClass = '',
$childrenWrapClass = '',
$limit = 0
) {
/* Showing Cms page About us at menu */
$page = $this->getCmspage('about-us');
if($page == null){
return;
}
$node = $this->nodeFactory->create(
[
'data' => [
'name' => $page->getTitle(),
'id' => $page->getIdentifier(),
'url' => $this->_urlBuilder->getUrl(null, ['_direct' => $page->getIdentifier()]),
'has_active' => false,
'is_active' => false // (expression to determine if menu item is selected or not)
],
'idField' => 'id',
'tree' => $subject->getMenu()->getTree()
]
);
$subject->getMenu()->addChild($node);
}
protected function getCmspage($identifier){
$page = $this->_pageFactory->create();
$pageId = $page->checkIdentifier($identifier, $this->_storeManager->getStore()->getId());
if (!$pageId) {
return null;
}
$page->setStoreId($this->_storeManager->getStore()->getId());
if (!$page->load($pageId)) {
return null;
}
if (!$page->getId()) {
return null;
}
return $page;
}
}
More detail can found article
copy file from
vendor/magento/module-theme/view/frontend/templates/html/topmenu.phtml
and put this file in your theme location
app/design/frontend/Vendor/YourTheme/Magento_Theme/templates/html/topmenu.phtml
Add below <li> structure just after <?php /* @escapeNotVerified */ echo $block->getChildHtml(); ?> line in topmenu.phtml
<li class="level0 level-top ui-menu-item">
<a href="<?php echo $this->getBaseUrl()."yourlink"; ?>" class="level-top ui-corner-all" role="menuitem">
<span><?php echo __("Your Custom Menu")?></span>
</a>
</li>
And more read link :-
How to add custom link in main menu in Magento 2?
Hope this help you
Thanks ...
<li class="level0 nav-3 level-top ui-menu-item">
– Mohit Patel
May 07 '20 at 05:39