0

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?

1
3

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>
13
  • Not working Sir. I am using Magento 2.3.0. And the theme is the Porto theme. Dec 16 '19 at 4:42
  • @ChandanSayed , Porto theme has the option to add menu item before and after the main menu, Please check in Megamenu Configurations. there is an option to assign block from after menu. Dec 16 '19 at 4:55
  • <body> <move element="sw.topnav" destination="store.menu" before="-"/> </body> Dec 16 '19 at 6:23
  • So you mean to apply my code you need to add <move element="sw.topnav" destination="store.menu" before="-"/> , Right? Dec 16 '19 at 6:28
  • app/design/frontend/Smartwave/porto/Smartwave_Megamenu default.xml <body> <move element="sw.topnav" destination="store.menu" before="-"/> </body> Dec 16 '19 at 6:30
1

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>

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.