Pergunta

How can we call a static block in Magento 2 in a page. In Magento 1 we have this with the following code:

<reference name="left">
<block type="cms/block" name="Klantenservice menu" before="-">
    <action method="setBlockId"><block_id>cmsmenu</block_id></action>
</block>    
</reference>
Foi útil?

Solução

If you want to call static block in page

Try below code :

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

If you want to call in phtml file :

Try below code :

<?php echo $block->getLayout()->createBlock(\Magento\Cms\Block\Block::class)->setBlockId('block_identifier')->toHtml();?>

Your xml file code should be :

<referenceContainer name="sidebar.additional">
   <block class="Magento\Cms\Block\Block" name="yourblockid">
       <arguments>
            <argument name="block_id" xsi:type="string">yourblockid</argument>
       </arguments>
   </block>
</referenceContainer>

At Last if you want to call phtml with your block in cms page :

Try below code :

{{block class="Magento\Modulename\Block\Blockname" template="Magento_Modulename::templatefilename.phtml"}} 

Refer this link for more details - https://chetansanghani.wordpress.com/2015/11/20/magento2-display-static-block-in-phtml-file-cms-page/

Outras dicas

This should work:

<referenceContainer name="sidebar.additional">
   <block class="Magento\Cms\Block\Block" name="yourblockid">
       <arguments>
            <argument name="block_id" xsi:type="string">yourblockid</argument>
       </arguments>
   </block>
</referenceContainer>

static Block Above image you can see my block identifier= "product_view_right_sidebar".

You need to only replace with your block identifier...

here, my block identifier = "product_view_right_sidebar" == "Your_block_identifier"

<referenceContainer name="sidebar.additional">
    <block class="Magento\Cms\Block\Block" name="product_view_right_sidebar">
        <arguments>
                <argument name="block_id" xsi:type="string">product_view_right_sidebar</argument>
        </arguments>
    </block>
</referenceContainer>
Licenciado em: CC-BY-SA com atribuição
Não afiliado a magento.stackexchange
scroll top