3

My name is Costin and I am a beginner Magento 2 developer. I have a question regarding on

How to add a button to header.phtml and to be displayed on the frontend?

What I have done until now:

  • I create a basic module in

/app/code/Vendor_Name_Module_Name

  • Copied the file

vendor/magento/module/theme/view/frontend/templates/html/header.phtml into /app/code/Vendor_Name_Module_Name/theme/view/frontend/templates/html/header.phtml.

what I want to achieve is : add button

Any help is much appreciated.

1
  • Any luck on showing the button ? Commented Nov 2, 2018 at 14:00

1 Answer 1

1

You can add this to your default.xml theme layout (inside the header.links) .

<referenceBlock name="header.links">
        <block class="Magento\Framework\View\Element\Html\Link" name="button.link"  before="-">
            <arguments>
                <argument name="label" xsi:type="string">BUTTON</argument>
                <argument name="path" xsi:type="string">buttonpath</argument>
            </arguments>
        </block>
    </referenceBlock>

Or create a cms block , if you want to display a socia media icon for example https://d.pr/free/i/RcbmCO

and in the default.xml add this inside header.links block

 <referenceBlock name="header.links">
        <!--added social media links-->
        <block class="Magento\Cms\Block\Block" name="social.media" before="-">
            <arguments>
                <argument name="block_id" xsi:type="string">social_media</argument>
            </arguments>
        </block>
    </referenceBlock><!-- end header.links -->
3
  • Thanks Ylgen, your answer helped me a lot .
    – Costin
    Commented Nov 5, 2018 at 7:32
  • @Costin Glad it helped :) Commented Nov 5, 2018 at 8:06
  • i want to add my url on button.please tell how it done? Commented Apr 15, 2019 at 10:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.