0

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?

Please See my attached files. enter image description here

Thanks In Advance

  • please check this answer, maybe it's help you: magento.stackexchange.com/questions/206572/… – Faisal Sheikh May 7 at 4:56
  • @FaisalSheikh thanks for reply, but this method is add in last menu, not work in middle menu – Gaurav K May 7 at 5:09
  • is last 4 link are custom links? – Amit Bera May 7 at 6:09
  • @AmitBera (our story,faq,our chaitable work) menus are static block, but where to buy is page link. – Gaurav K Jul 20 at 4:26
1

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

| improve this answer | |
-1

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 ...

| improve this answer | |
  • Hey Mohit thanks for reply, but this method is add in last menu, not work in middle menu. – Gaurav K May 7 at 5:10
  • I am no sure but try first line add update line <li class="level0 nav-3 level-top ui-menu-item"> – Mohit Patel May 7 at 5:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.