Calling static blocks from anywhere in Magento

Posted on

Magento eCommerce gives us the power to add our own snippets of html wherever we wish. Magento calls these snippets ‘static blocks’. To create a new block go to CMS > Static Blocks at the top right of this list click ‘Add New Block’.

Enter your title and unique identifier e.g. ‘top-advert’. Set the block to enabled and enter the content of the block into the WYSIWYG editor. This is all you need to do Magento side. To call the block onto the frontend of the website open up your template file. For example if I want to call my advert into my product listing I would open:

[pyg language=”bash”]
/app/design/frontend/theme/default/template/catalog/product/list.phtml
[/pyg]

I want my advert to appear above the product list so I find the opening html for the products list and put this before it:

[pyg language=”php”]
getLayout()->createBlock(‘cms/block’)->setBlockId(‘top-advert’)->toHtml() ?>
[/pyg]

Thats it, everything you need to know 🙂

Want to discuss a project?

Talk to our Magento experts on 01785 279920

Request a callback