Tuesday, April 24, 2012

Magento add category with images on homepage



if you would like to add category titles with images on the front page, just follow the steps:

place this snippet on your homepage cms page:

{{block type="core/template" name="homepage" template="catalog/category/homepagecategory.phtml"}}

then place this code in app/code/design/frontend/YOUR_PACKAGE/YOUR_TEMPLATE/catalog/category/homepagecategory.phtml

<?php
 $currcatId = 3;
 if($currcatId != NULL)
    {
  $collection = Mage::getModel('catalog/category')->getCategories($currcatId);
  $ctr = 0;
  foreach($collection as $subcat)
  {
      //limits the categories to be shown on home page to 6 categories
      if ($ctr < 6)
   if($subcat->getIsActive())
   {
       $category = Mage::getModel('catalog/category')->load($subcat->getEntityId());
       $ctr++;
       if (strtolower($category->getName()) == "overige lampen") 
       {
       }
    else
   {
?>

<div class="sub-category-container" style="margin-right:<?php if(($ctr % 2) == 0) {echo "0";} else {echo "8px";} ?>; margin-bottom:10px;" >
 <a href="<?php echo $this->getBaseUrl() . $category->getUrlKey() ?>" style="background:none; border:none;">
  <div>
     <?php if($_imgUrl = $category->getImageUrl()): ?>
   <img src="<?php echo $_imgUrl; ?>" border="0" />
     <?php else:?>
   <img src="<?php echo $this->getSkinUrl('images/lampen/placeholder.jpg') ?>" border="0" width="185px" height="185px" />
     <?php endif; ?>
  </div>
  <div>
   <a href="<?php echo $this->getBaseUrl() . $category->getUrlKey() ?>"><?php echo $category->getName(); ?></a>
  </div>
 </a>
</div>

<?php
     }
   }
  }
 }
?>

3 comments:

  1. There is no app/code/design ?
    Where to put it in Magento 1.7 ?

    ReplyDelete
  2. Hi,

    Unfortunately it's not working for me on Magento 1.7.0.2
    How can I display only specific categories ?
    Thanks.

    ReplyDelete
  3. Nice blog. Thank you so much for the post. Found helpful. Lately came across an guide which helped me: http://apptha.com/blog/finest-collection-of-magento-resources-for-developers-and-ecommerce-store-owners/

    ReplyDelete