6

How can I add a block to the magento 2 luma header like in this image? I'm using M2.2 and app/design/frontend/magento directory is empty (i see that referenced a lot).

enter image description here

3
  • Check your vendor/magento/theme-frontend-luma/
    – BartZalas
    Aug 4, 2018 at 22:53
  • are you using any custom theme Or default Luma Theme ?
    – Pawan
    Aug 5, 2018 at 5:00
  • please accept answer if it's working. So, other developer can use this answer :) Aug 5, 2018 at 16:17

2 Answers 2

2

Add below code at after top.search referenceContainer:

app/code/design/frontend/[Package]/[Theme]/Magento_Search/layout/default.xml

    <referenceContainer name="header-wrapper">
        <block class="Magento\Cms\Block\Block" name="Promo">
            <arguments>
                <argument name="block_id" xsi:type="string">topPromo</argument>
            </arguments>
        </block>
    </referenceContainer>

Note: Need to change block id topPromo with your block Id and need some CSS for positioning.

2
  • Welcome @ Will :)
    – Pawan
    Aug 5, 2018 at 17:52
  • 1
    You can also upvote so question can be consider as close :)
    – Pawan
    Aug 5, 2018 at 17:57
3

Add this code in your default.xml file at below path

/app/code/design/frontend/MyCompany/MyTheme/Magento_Search/layout/default.xml

<?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>
        <referenceContainer name="header-wrapper">
            <block class="Magento\Framework\View\Element\Template" name="customFile" template="Magento_Search::customFile.phtml" />
        </referenceContainer>
</body>

Now, create your template file at below path

/app/code/design/frontend/MyCompany/MyTheme/Magento_Search/templates/customFile.phtml

<div class="block" style="float: right;margin-bottom: 0.5%;margin-top: 0.5%;">
    <?php echo __('This is Custom Text File'); ?>
</div>

Hope, It may be helpful for you.

4
  • 1
    Why not just us a CMS Widget to place a CMS static block there. Then no code is needed Aug 5, 2018 at 6:25
  • Exactly. You are right. But, it's just a one type of solution I added. Aug 5, 2018 at 6:28
  • How do you get a cms static block to show in the header between the logo and the search? Aug 5, 2018 at 13:14
  • reference container header-wrapper is used for display in center. I add phtml into this container and add some css. Aug 5, 2018 at 14:00

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.