I want to add a static block on the left side of all pages.
Can anyone tell me how to do that?
Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. It only takes a minute to sign up.
Sign up to join this communityFirst 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>
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/