Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display categories on home page in Magento

I am a newbie in Magento and wanted to display all categories on the Magento home page rather than a top nav menu.

I read a lot of articles on it, but nothing has helped. Please point me to the right direction.

Thanks in advance.

like image 954
Aisha Avatar asked Dec 27 '22 23:12

Aisha


2 Answers

The code below does a couple of things, first it will get all the store’s categories - it then checks to see if they are active before continuing.

Code Courtesy : magentocommerce forum

   <?php
     /* Get the categories that are active for the store */
      $_main_categories=$this->getStoreCategories();
      /* Get the current category the user is in */
      $_current_category=$this->getCurrentCategory();
      /* Get the current category path */
      $_categorypath = $this->getCurrentCategoryPath();
      ?>
      <ul>
      <?php
      if ($_main_categories):
          /* This bit cycles through the categories - setting the next one to current */
          foreach ($_main_categories as $_main_category):
            if($_main_category->getIsActive()):                             
          $cur_category=Mage::getModel('catalog/category')->load($_main_category->getId());
                 $layer = Mage::getSingleton('catalog/layer');
                 $layer->setCurrentCategory($cur_category);     
      /* Write the main categories */           
      ?>               
      <li><a href="<?php echo $this->getCurrentCategory()->getUrl()?>"><?php echo $this->getCurrentCategory()->getName();?></a></li>   
      <?php
      /* Check the category variable loop against the current category path if it is - print sub categories */
      if (in_array($this->getCurrentCategory()->getId(), $_categorypath)): ?>
      <?php $_maincategorylisting=$this->getCurrentCategory()?>                       
      <?php $_categories=$this->getCurrentChildCategories()?>
      <?php if($_categories->count()):?>
      <ul>
      <? foreach ($_categories as $_category):?>                   
         <? if($_category->getIsActive()):                   
            $cur_subcategory=Mage::getModel('catalog/category')->load($_category->getId());
                 $layer = Mage::getSingleton('catalog/layer');
                 $layer->setCurrentCategory($cur_subcategory); 
      ?>                         
      <li><a href="<?php echo $this->getCategoryUrl($_category)?>"> <?php echo $_category->getName()?></a></li>
          <? endif;?>
      <?endforeach?>
      </ul>           
      <?php /* This resets the category back to the original pages category
      ****     If this is not done, subsequent calls on the same page will use the last category
      ****    in the foreach loop
      */   ?>
      <?php $layer->setCurrentCategory($_current_category);  ?>
      <?endif;?>   
      <?endif;?>                                   
      <?php         
      endif;
      endforeach;
      else:
      ?>
      <p>$_main_categories array was empty.</p>
      <p>This might be because you are referencing this phtml file with a wrong type attribute. You should use <block type="catalog/navigation" ... /> !</p>
      <?php endif; ?>
like image 121
Vijay Avatar answered Jan 11 '23 21:01

Vijay


I got where the problem was. The solution is at this link http://samsami2u.wordpress.com/2009/09/15/add-categories-with-images-on-homepage-magento/ and I was trying to add this line {{block type="catalog/navigation" name="catalog.category" template="catalog/category/list.phtml"}} to home page from back end in layout update xml under design tab but the correct way was to place it in content tab.

like image 32
Aisha Avatar answered Jan 11 '23 20:01

Aisha