Pages

Tuesday 2 December 2014

Magento Displaying CMS Static Block based on Category ID

1- open developer log to check the listing page layout.
2- In my case i found that it is 3columns.phtml
app/design/frontend/default/your-theme-name/template/page/3columns.phtml
Now in the Magento back end create a new Static Block. I will assume that you are already familiar with creating a Static Block in Magento so will not go into how to do this. If you are not familiar with creating Static Blocks please go to the Magento Knowledge Base
"http://www.magentocommerce.com/knowledge-base/entry/how-do-i-create-and-edit-static-blocks"
Decide where you would like your banner to be displayed in my case after the breadcrumbs then add this code.
<?php $category = Mage::getModel('catalog/layer')->getCurrentCategory();?>
<?php 
$myValidIds = array(3,6);
if(in_array($category->getId(), $myValidIds)):
?>
<?php
echo $this->getLayout()->createBlock('cms/block')->setBlockId('my-sidebar-1')->toHtml();
echo $this->getLayout()->createBlock('cms/block')->setBlockId('my-sidebar-2')->toHtml();
?>
<?php endif; ?>
Firstly you will need to change the numbers in the $myvalidid variable array. Here you will need to add the ID’s of the categories that you would like the Static Block to be visible in separated by comma. Your category ID’s can be found in the Magento back end.
Catalog > Manage Categories
Finally add the identifier for the Static Block you wish to be called into the page.
Save and upload the template file back into your Magento installation.

No comments:

Post a Comment