Skip to main content
edited body; edited tags
Source Link
Amit Bera
  • 77.7k
  • 20
  • 124
  • 239

Here's my code:

Defaultdefault.xml file in app/code/Demo/Mymodule/view/frontend/layout

<?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">
            <block class=”Demo\Mymodule\Block\Link" name="custom-header-link" >
                <arguments>
                    <argument name="label" xsi:type="string" translate="true">test</argument>
                    <argument name="path" xsi:type="string" translate="true">Test</argument>
                </arguments>
            </block>
        </referenceBlock>
    </body>
</page>

Link.php in app/code/Demo/Mymodule/Block:

<?php
 
namespace Demo\Mymodule\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>';
    }
}

But it's not showing!

Here's my code:

Default.xml file in app/code/Demo/Mymodule/view/frontend/layout

<?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">
            <block class=”Demo\Mymodule\Block\Link" name="custom-header-link" >
                <arguments>
                    <argument name="label" xsi:type="string" translate="true">test</argument>
                    <argument name="path" xsi:type="string" translate="true">Test</argument>
                </arguments>
            </block>
        </referenceBlock>
    </body>
</page>

Link.php in app/code/Demo/Mymodule/Block:

<?php
 
namespace Demo\Mymodule\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>';
    }
}

But it's not showing!

Here's my code:

default.xml file in app/code/Demo/Mymodule/view/frontend/layout

<?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">
            <block class=”Demo\Mymodule\Block\Link" name="custom-header-link" >
                <arguments>
                    <argument name="label" xsi:type="string" translate="true">test</argument>
                    <argument name="path" xsi:type="string" translate="true">Test</argument>
                </arguments>
            </block>
        </referenceBlock>
    </body>
</page>

Link.php in app/code/Demo/Mymodule/Block:

<?php
 
namespace Demo\Mymodule\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>';
    }
}

But it's not showing!

Source Link

Add Custom Header Link in Magento 2 - Not Working

Here's my code:

Default.xml file in app/code/Demo/Mymodule/view/frontend/layout

<?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">
            <block class=”Demo\Mymodule\Block\Link" name="custom-header-link" >
                <arguments>
                    <argument name="label" xsi:type="string" translate="true">test</argument>
                    <argument name="path" xsi:type="string" translate="true">Test</argument>
                </arguments>
            </block>
        </referenceBlock>
    </body>
</page>

Link.php in app/code/Demo/Mymodule/Block:

<?php
 
namespace Demo\Mymodule\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>';
    }
}

But it's not showing!