4

enter image description here

How can add a custom page link in the header Before the Welcome Message

  • 2
    I donot think this is a duplicated answer – Amit Bera Mar 18 at 6:53
1

Please check on below URL. it shows how to add custom blog link.

Let me know if it helps.

https://zemez.io/magento/support/how-to/magento-2-1-x-manage-top-header-links/

Also, need to update theme default.xml file with below code.

<referenceBlock name="header.panel">
<block class="Magento\Cms\Block\Block" name="header_promo_top">
   <arguments>
       <argument name="block_id" xsi:type="string">header_promo_top</argument>
   </arguments>
</block>
</referenceBlock>
  • 2
    Please donot p put a single link answer.Answer must has some content – Amit Bera Mar 18 at 6:55
  • @AmitBera, Thank you. i will take care from next time. – Yogesh Mar 18 at 7:09
2

Step 1: create a CMS page. For example, we create a “Custom Link” page with an URL such as http://localhost/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="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_name\Module_name\Block\Link" name="some_link" >
            <arguments>
                <argument name="label" xsi:type="string" translate="true">Custom link</argument>
                <argument name="path" xsi:type="string">test</argument>
            </arguments>
        </block>
    </referenceBlock>
</body>

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

<?php
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>';
}
}

I hope it helps!

1

Add this code in default.xml

 <referenceBlock name="header.links">
                <block class="Magento\Customer\Block\Account\RegisterLink" name="register-link-new"  after="-">
                    <arguments>
                        <argument name="label" xsi:type="string" translate="true">Create an Account</argument>
                    </arguments>
                </block>
                <block class="Vendor\Module\Block\Toplink" name="add.subscriptionplan.top" ifconfig="Vendor_Module/general/enable" after="-" />   
            </referenceBlock>
            <referenceBlock name="register-link" remove="true"/> 
1

@Chirag Patel code almost right.

Only have to add below code after

<move element="some_link" destination="header.links" before="-"/>

or

<move element="some_link" destination="header.links" before="header"/>

after </referenceBlock> tab on default.xml.

It will move your link at first

  • Thanks @Amit Bera for make good answer to perfect answer. +1 for this. :) – Chirag Patel Mar 18 at 7:22

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.