Add Top Link Magento 2

You can add a new top link as follows

1) Add the below code in layout XML

File : app\code\PHPcodez\Subscription\view\frontend\layout\default.xml

<?xml version="1.0"?>
<page xmlns:xsi="" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
 <referenceBlock name="header.links">
 <block class="PHPCodez\Subscription\Block\Link" name="add-new-header-link">
 <argument name="label" xsi:type="string" translate="true">New Link</argument>
 <argument name="path" xsi:type="string" translate="true">new-link</argument>

2) Define the block Link.php

File : app\code\PHPcodez\Subscription\Block\Link.php

 namespace PHPCodez\Subscription\Block;
class Link extends \Magento\Framework\View\Element\Html\Link {

protected function _toHtml() {
 if (false != $this->getTemplate()) {
 return parent::_toHtml();
 return '<li class="level0 "><a ' . $this->getLinkAttributes() . ' class="level-top" >' . $this->escapeHtml($this->getLabel()) . '</a></li>';

Leave a Reply

Your email address will not be published. Required fields are marked *