How to add links to header top in magento 2. i have tried to

<?xml version="1.0"?> <!-- /** * Copyright © 2015 Magento. All rights reserved. * See COPYING.txt for license details. */ --> <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"> <block class=vendor\HelloWorld\Block\Link" name="add-new-header-link"> <arguments> <argument name="label" xsi:type="string" translate="true">New Link</argument> <argument name="path" xsi:type="string" translate="true">new-link</argument> </arguments> </block> </referenceBlock> </body>

but that does not seems to work


First you need to create a custom module for that you can follow this tutorial:-


after above step now you need to follow few more steps:-

Step 1: We will create a CMS page. For example, we create a “Custom Link” page with an URL such as //domain.com/custom_link.

Step 2: Create a default.xml file in the following path: app\code\Vendor\Module\view\frontend\layout with the following content:

<?xml version="1.0"?>
<page xmlns:xsi="//www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
        <referenceBlock name="header.links">
            <block class=”Vendor_name\Module_name\Block\Link" name="some_link" >
                    <argument name="label" xsi:type="string" translate="true">Custom link</argument>
                    <argument name="path" xsi:type="string">test</argument>

For more details, the previous xml is used to register a Block Class in “Vendor_name\Module_name\Block\Link” . Then, set Label and URL path for it in the arguments tag.

Step 3: Create the Block class Link with the following content:

namespace Vendor\Module\Block;
class Link extends \Magento\Framework\View\Element\Html\Link
     * Render block HTML.
     * @return string
    protected function _toHtml()
        if (false != $this->getTemplate()) {
            return parent::_toHtml();
        return '<li><a ' . $this->getLinkAttributes() . ' >' . $this->escapeHtml($this->getLabel()) . '</a></li>';

Now just run the below command from your magento root directory

  • <root>php bin/magento setup:upgrade
  • <root>php bin/magento setup:static-content:Deploy -f
  • <root>php bin/magento cache:clean YOUR done!
| improve this answer | |
  • thank you it works – user84154 Jun 30 at 6:18
  • you are most welcome buddy – Pramod Jun 30 at 6:36

Your Answer

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