1

Add a new CMS block on top of the homepage (header) and another one at the bottom of the homepage (footer) using XML layout.

2 Answers 2

1

You have to override the core files in your theme or module. I'm overriding in my module have a look into this. Maybe it helps you.

view/frontend/layout/cms_index_index.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.panel">
            <block class="Magento\Framework\View\Element\Template"
                   template="Vendor_Module::html/header.phtml"/>
        </referenceContainer>
        <referenceContainer name="footer">
            <block class="Magento\Framework\View\Element\Template"
                   template="Vendor_Module::html/footer.phtml"/>
        </referenceContainer>
    </body>
</page>

after creating file in layouts simply add header and footer file in templates folder

view/frontend/templates/html/footer.phtml

and

view/frontend/templates/html/header.phtml

1

You can add the static blocks in the homepage header and footer by adding the below code in your theme.

<?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.panel">
            <block class="Magento\Cms\Block\Block" name="header_block">
                <arguments>
                    <argument name="block_id" xsi:type="string">header_block</argument>
                </arguments>
            </block>
        </referenceContainer>
        <referenceContainer name="footer">
            <block class="Magento\Cms\Block\Block" name="footer_block">
                <arguments>
                    <argument name="block_id" xsi:type="string">footer_block</argument>
                </arguments>
            </block>
        </referenceContainer>
    </body>
</page>

Where header_block is the name of the CMS block of the header and footer_block is the name of the footer CMS block.

Furthermore, if you want to add the template files instead of CMS blocks then there is no need to add the block default class in the XML from Magento 2.3. So the xml code will look like as follows:

<?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.panel">
            <block name="header_block" template="Vendor_Module::html/header.phtml"/>
        </referenceContainer>
        <referenceContainer name="footer">
            <block name="footer_block" template="Vendor_Module::html/footer.phtml"/>
        </referenceContainer>
    </body>
</page>

The theme's file path should be:

app/design/frontend/Vendor/Theme/Magento_Theme/layout/cms_index_index.xml

Hope it helps!!!

2

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.