Add a custom navbar menu
-
24-03-2021 - |
Question
I am trying to add a custom link(contact us) in navbar menu and wanted to add the contact us page link. how to add that?
Solution
You can add the below code in theme default.xml file
<referenceBlock name="catalog.topnav">
<block class="Magento\Framework\View\Element\Html\Link" name="custom.contact.link">
<arguments>
<argument name="label" xsi:type="string" translate="true">Contact us</argument>
<argument name="path" xsi:type="string" translate="true">contact</argument>
<argument name="class" xsi:type="string" translate="true">custom-contact-link</argument>
</arguments>
</block>
</referenceBlock>
OTHER TIPS
Add Custom Header Links
Step 1: Create a layout XML file in the below path for add custom header. app/code/VendorName/ModuleName/view/frontend/layout/default.xml
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceBlock name="header.links">
<move element="custom-link" destination="header.links"/>
<block class="VendorName\ModuleName\Block\Header" name="custom-link" after="my-account-link"/>
</referenceBlock>
</body>
</page>
Step 2: Create a block file in the below path that is referred in the layout file. app/code/VendorName/ModuleName/Block/Header.php
<?php
namespace VendorName\ModuleName\Block;
class Header extends \Magento\Framework\View\Element\Html\Link
{
protected $_template = ‘VendorName_ModuleName::link.phtml';
public function getHref() {
return__( 'contactus');
}
public function getLabel() {
return __('Contact Us');
}
} ?>
Step 3: Create a template file in the below path that is referred to the block file. app/code/VendorName/ModuleName/view/frontend/templates/link.phtml
<li>
<a <?php echo $block->getLinkAttributes() ?>><?php echo $block->escapeHtml($block->getLabel())?></a>
</li>
Add Custom Footer Links:
Step 1: Create a layout XML file in the below path for add custom footer.
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceBlock name="footer_links">
<block class="Magento\Framework\View\Element\Html\Link\Current" name="custom-link-2">
<arguments>
<argument name="label" xsi:type="string">Custom Footer Link</argument>
<argument name="path" xsi:type="string">custom_footer_link</argument>
</arguments>
</block>
</referenceBlock>
</body>
</page>