Question

How to show a block content on homepage?

After install magento2 without sample data, I chose 'Magento Luma' theme and add 'Home Page Block' block and copy the code from the sample store.

but nothing show up on the home page, how to show my cms block on home page like sample store?

my home enter image description here

sample store

enter image description here

Était-ce utile?

La solution

in admin panel go to Content->Pages and edit home page and if you created your static block from admin panel add following to the Content tab

{{block class="Magento\Cms\Block\Block" block_id="your_block_identifier"}}

and if you created a custom block using a module add following to home page content tab

{{block class="Vendor\ModuleName\Block\BlockName" template="Vendor_ModuleName::yourcustomblock.phtml"}} 

then save the changes and flush magento cache and deploy/delete static contents now refresh home page .

If this was not worked let me know

Autres conseils

try this or else post your code then we will suggest you where you went wrong.

Block file

<?php
namespace Learning\SmartSearch\Block;

class SmartSearch extends \Magento\Framework\View\Element\Template
{
    public function getTitle()
    {
        return "Smart Search";
    }
}

etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
    <module name="Learning_SmartSearch" setup_version="1.0.0">
    </module>
</config>

view/layout/cms_index_index.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="content">
            <block class="Learning\SmartSearch\Block\SmartSearch" template="Learning_SmartSearch::html/cms.phtml"/>
        </referenceContainer>
    </body>
</page>

view/templates/html/cms.phtml

<h1><?php echo $block->getTitle(); ?></h1>

after creating flush cache.

I hope this will help you. If you are not getting results let me know.

In the home page you can call the static block like the below mentioned way

{{block class="Magento\Cms\Block\Block" block_id="your_block_identifier"}}

In the html page you can call the static block like the below mentioned way.

<?php echo $this->getLayout()->createBlock('Magento\Cms\Block\Block')->setBlockId('block_identifier')->toHtml();?>
Licencié sous: CC-BY-SA avec attribution
Non affilié à magento.stackexchange
scroll top