Magento Tips that will help you lot while working on magento theming

Magneto theming is little bit different from other CMS. Today I’ll show you some tips, that will help you lot while working on magento theming.

Display single category products on home page.

To do this first of all create any phtml file in your theme directory and write following code in it.

1
2
3
4
5
6
7
8
9
<?php
$products = Mage::getModel("catalog/category")->load(2)->getProductCollection();
 ?>
<ul class=”product list”>
foreach($product_collection as $product) {
 echo “<li><a href=’”.$product->getProductUrl().”’>”.$product->getName().”</a>”;
}
?>
</ul>

After that Now go to admin panel and go to cms->pages, Here you will see all pages. Now click on home page and go to content section

In content enter following code to include above created phtml in home page

1
{{block type="catalog/product" template="catalog/product/homepage.phtml"}}

Where “catalog/product/homepage.phtml” is location of file which we created in first step.

Calling CMS static block in phtml file.

In some cases you need some static html on left or right sidebar and that should be editable from admin nbso panel. To this you have to create a new static block in magneto admin and call that block in any phtml.

You can call any cms static block inside phtml file by following way

1
2
 
<?php echo $this->getLayout()->createBlock("cms/block")->setBlockId("block_code")->toHtml() ?>

Where “block_code” is unique code defined, while creating block in admin section of magento.

Displaying all categories name by parent category id

in some cases, while you are working on theme implementation of magento, you need to show child categories on any page by parent category id.

You can do this by writing following code in your phtml file

1
2
3
4
5
6
7
8
9
<?php
$_categories=Mage::getModel(‘catalog/category’)->getCategories(10);
?>
<ul class=”category-list”>
foreach($_caetgories as $_category){
echo “<a href=’”.$_category->getUrl().”’>”.$_category->getName().”</a>”;
}
?>
</ul>

Where “10” is parent category id.

Hope these 3 tips helped you lots while working on magento theming.

There are still lot more tips, which will help you while working on theming in magento. I’ll be back later in another posts with other tips of magento theming.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.


Skip to toolbar