Magento Expert Forum - Improve your Magento experience

Results 1 to 2 of 2

How to add custom top links in header

  1. #1
    Junior Member john_here's Avatar
    Join Date
    Dec 2016
    Posts
    149
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to add custom top links in header

    HOW TO ADD CUSTOM TOP LINKS IN HEADER

    View more Magento 2 Themes

    Name:  add-custom-top-links-header.jpg
Views: 11
Size:  374.6 KB

    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:


  2. #2
    Junior Member
    Join Date
    Sep 2016
    Posts
    228
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default

    Thanks mate for describing in brief, I never come up with this kind of coding before.


    Team Building
    Team Building Games
    Team Building Activities
    Corporate Team Building
    Indoor Team Building Activities
    Last edited by vishnu; 13-05-2019 at 12:17 PM.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •