HOW TO ADD CUSTOM TOP LINKS IN HEADER
View more Magento 2 Themes
In this post we will show you how to add a contact link in magento 2
top links header.
Firstly, you need to create a CMS page for example you create an “Contact Us” page with identifier as “contacts”
Secondly, link access to contact page http://domain.com/contacts
Thirdly, to add magento 2 top links, you need to add following code in default.xml of your theme
Add code in body tag of a file *.xml. For example: app/design/frontend/Magento/YOUR_THEME/Magento_Theme/layout/default.xml
PHP Code:
<referenceBlock name="header.links">
<block class="Magento\Framework\View\Element\Html\Link" name="contact-link">
<arguments>
<argument name="label" xsi:type="string" translate="true">Contact Us</argument>
<argument name="path" xsi:type="string" translate="true">contacts</argument>
</arguments>
</block>
</referenceBlock>
The previous code will be redirected to “Magento\Framework\View\Element\Html\Link”
class and tranfers label = Contact Us and path = contacts(http://domain.com/contacts)
After that, Check the PHP Magento\Framework\View\Element\Html\Link.php file,
there is one link shown on frontend
PHP Code:
protected function _toHtml()
{
if (false != $this->getTemplate()) {
return parent::_toHtml();
}
return '<li><a ' . $this->getLinkAttributes() . ' >' . $this->escapeHtml($this->getLabel()) . '</a></li>';
}
VIEW DETAIL->
View more threads in the same category:
Bookmarks