12

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>

4 Answers 4

56

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/

9
  • Thanks for the reply, whatever I try in a page I get the following error: "Please correct the XML data and try again. Element 'layout': Character content other than whitespace is not allowed because the content type is 'element-only'. Line: 1" Any idea?
    – Coen
    Commented Jan 25, 2017 at 13:42
  • @Coen check is there any extra space in your xml file ? or at the starting of xml file ? Commented Jan 25, 2017 at 13:43
  • yes there was a comma, I now found somewhere else the answer. See my question above
    – Coen
    Commented Jan 25, 2017 at 13:47
  • @Coen updated the answer please check Commented Jan 25, 2017 at 13:49
  • Thanks, uses the one in the xml file and that one by a cms page
    – Coen
    Commented Jan 25, 2017 at 15:16
8

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>
2
  • Where should I put this xml? Commented Dec 19, 2017 at 18:02
  • In the layout file used for the side bar
    – diazwatson
    Commented Jun 23, 2020 at 22:37
0

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>
-1

You can call the static block with the help of widget. This is one of the good feature of magento.

Here is the list of widgets. We need to create new one so click on “Add Widget” button.

Select type as “CMS Static Block” and choose Design Theme and click on “Continue” button. enter image description here

Enter the Widget title and also assign the Store View where you want this block to appear. Also the sort order which displays it in order in list of widgets.

This is very important and good that we can select the specific page or group of pages where we can display this static block.

enter image description here Now select our static block created in the widget option. and Save the changes. enter image description here Lets see the static block on the pages. enter image description here Simple !! Do try on your site.

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.