4

I want to add a static block on the left side of all pages.

Can anyone tell me how to do that?

1

2 Answers 2

2

First of all, override the 2columns-left.xml in your theme module and define every page's column layout as 2column-left from XML and cms pages. Than put following code in your 2columns-left.xml file

<referenceContainer name="columns">
    <container name="div.sidebar.main" htmlTag="div" htmlClass="sidebar sidebar-main" after="main">
    <container name="sidebar.main" as="sidebar_main" label="Sidebar Main"/>
       <block class="Magento\Cms\Block\Block" name="blockname">
         <arguments>
            <argument name="block_id" xsi:type="string">cmsblock-identifier</argument>
         </arguments>
       </block>
    </container>
    <container name="div.sidebar.additional" htmlTag="div" htmlClass="sidebar sidebar-additional" after="div.sidebar.main">
        <container name="sidebar.additional" as="sidebar_additional" label="Sidebar Additional"/>
    </container>
</referenceContainer>

Above code will display your static block in the left side of your every 2columns-left style page.

The code to add cms block is:

       <block class="Magento\Cms\Block\Block" name="blockname">
            <arguments>
                <argument name="block_id" xsi:type="string">cmsblock-identifier</argument>
            </arguments>
       </block>
1
  • My pleasure.. :) @AlinLupoiu Aug 4, 2017 at 11:28
6

If you want to call static block in page in magento 2

Try this below code :

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

If you want to call in phtml file :

Try this below code :

<?php echo $block->getLayout()->createBlock('Magento\Cms\Block\Block')->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 this 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/

1
  • You can edit column files.
    – DevAnd
    Aug 7, 2017 at 18:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.