How to include CMS block in template file(.phtml)
4 Answers
Use this code to call cms block in phtml file:
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('block-id-here')->toHtml()?> If you created the cms static block like "my_block_id", Use the below code:
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('my_block_id')->toHtml(); ?> Well this question answered many times in this forum, I dont know why you ask same question again,
but you can try below things.
if your template(PHTML)'s block class extends mage_core_block_template at some where then you can easily use below code
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('Your_CMS_Block_Identifier')->toHtml(); ?> IF your Templeate's block class does not extends mage_core_block_template at some point then you can try below code in your phtml file
<?php echo Mage::app()->getLayout()->createBlock('cms/block')->setBlockId('Your_CMS_Block_Identifier')->toHtml(); ? - Is there any way to fetch the cms block using the "ID" not the identifier ?Anish Karim– Anish Karim2018-10-04 11:29:57 +00:00Commented Oct 4, 2018 at 11:29
- 1@AnishKarim try this
<?php echo Mage::app()->getLayout()->createBlock('cms/block')->setId('Your_CMS_Block_Id')->toHtml(); ?2018-10-04 12:38:19 +00:00Commented Oct 4, 2018 at 12:38
You can also do it like this:
<your_layout_handle> <reference name="root"> <block type="cms/block" name="block.name"> <action method="setBlockId"><block_id>cms_block_id_</block_id></action> </block> </reference> <your_layout_handle> In your phtml you put this where you want to display it:
<?php echo $this->getBlockHtml('block.name') ?>