Thursday, September 9, 2010

Display Categories in the Home page



  • Display Categories in the Home page
  • Display a random product's picture with the category name
  • It can only fetch the 1st level category

-----------------------------------------------------------------------------------------------------
/* PUT THIS CODE IN CATALOG/CATEGORY/LIST.PHTML (create it if not present) */

<?php // Iterate all categories in store
    $limit = 0;
    $_helper    = $this->helper('catalog/output');
    foreach ($this->getStoreCategories() as $_category):

        // If category is Active
        if($_category->getIsActive()):


            // Load the actual category object for this category
            $cur_category = Mage::getModel('catalog/category')->load($_category->getId());

            if ($_imgUrl = $cur_category->getImageUrl()){

                $_imgHtml = '<img src="'.$_imgUrl.'" alt="'.$this->htmlEscape($cur_category->getName()).'" title="'.$this->htmlEscape($cur_category->getName()).'"  width="105px" />';
                $_imgHtml = $_helper->categoryAttribute($cur_category, $_imgHtml, 'image');
            } ?>
             <div class="home-category">
                <div class="linkimage">
                    <a href="<?php echo $this->getCategoryUrl($cur_category) ?>" style="border:none">
                    <?php echo $_imgHtml; ?>
                    </a>
                </div>
                <div class="category-link-container">
                <a href="<?php echo $this->getCategoryUrl($cur_category) ?>">
                       <?php echo $_helper->categoryAttribute($cur_category, $cur_category->getName(), 'name') ?>
                </a>
                </div>
            </div>
<?php
           $limit+=1;
        endif;

        if ($limit==8):
            break;
        endif;

    endforeach;
?>

----------------------------------------------------------------------------------------------------------
/* PUT THIS CODE IN THE CMS/PAGES/HOME*/
/* IF YOU WANT TO HARDCODE IT PLACE THIS IN LAYOUT/PAGE.XML -> "Custom page layout handles" AREA */

{{block type="catalog/navigation" name="catalog.category" template="catalog/category/list.phtml"}}
----------------------------------------------------------------------------------------------------------


Reference

No comments:

Post a Comment