Monday 6 May 2013

Show all category subcategory with thumbnail images in magento


First create cms static block in magento back-end and put this code in your static block
{{block type="catalog/navigation" name="catalog.category" template="catalog/category/myallcat.phtml"}}
----------------------------------------------------------------------------------------------
How to call your static block in phtml page and cms page like home page
In CMS page: <div>{{block type="cms/block" block_id="your block id"}}</div>
in phtml file: echo $this->getLayout()->createBlock('cms/block')->setBlockId('block id')->toHtml();
--------------------------------------------------------
Create PHTML file for showing category with thumbnail in directory:
app/design/frontend/default/default/template/catalog/category/myallcat.phtml
And past my bellow code:

<ul>
<?php
$children = Mage::getModel('catalog/category')->getCategories(2); // put your main root category id here
foreach ($children as $category):
$category=Mage::getModel('catalog/category')->load($category->getId());
echo '<li><a href="' . $category->getUrl() . '">' . $category->getName() . '</a></li>'; ?>
<img src="<?php echo Mage::getBaseUrl('media')?>catalog/category/<?php echo $category->getThumbnail() ?>" height="100" width='100'>
<?php 
$children = Mage::getModel('catalog/category')->getCategories($category->getId());
foreach ($children as $category):
$category=Mage::getModel('catalog/category')->load($category->getId());
echo '<li><a href="' . $category->getUrl() . '">' . $category->getName() . '</a></li>'; ?>
<img src="<?php echo Mage::getBaseUrl('media')?>catalog/category/<?php echo $category->getThumbnail() ?>" height="100" width='100'>

<?php 
$children = Mage::getModel('catalog/category')->getCategories($category->getId());
foreach ($children as $category):
$category=Mage::getModel('catalog/category')->load($category->getId());
echo '<li><a href="' . $category->getUrl() . '">' . $category->getName() . '</a></li>'; ?>
<img src="<?php echo Mage::getBaseUrl('media')?>catalog/category/<?php echo $category->getThumbnail() ?>" height="100" width='100'>
<?php
endforeach;
?>

<?php
endforeach;
?>


<?php
endforeach;
?>
</ul>

9 comments:

  1. Great Work Brother...!! very helpful..!!

    ReplyDelete
  2. Yes it works, but this other method is easier and better: http://www.creare.co.uk/magento-subcategories-category-pages.

    ReplyDelete
  3. Thank you.
    Its working.
    I wasted around 2 days of mine for this.
    Thanks a lot.

    ReplyDelete
  4. It worked man Thanks

    ReplyDelete
  5. not working in 1.9.1

    ReplyDelete
  6. Phew thank you - This has been causing me an issue all day!

    ReplyDelete
  7. Great blog. All posts have something to learn. Your work is very good and i appreciate you and hoping for some more informative posts.keep writing
    magento development company in bangalore 

    ReplyDelete