Magento 1.9 – Show All Categories Down the Side of Home Page

categorymagento-1.9

Im using code that i found on a magento wiki page about showing All categories (http://www.magentocommerce.com/wiki/4_-_themes_and_template_customization/catalog/getting_and_using_categories_and_subcategories).

However when i apply this to a page it just shows the words "All categories" which is a link to my categories page. But i want it to display all of my categories (the sub categories of All categories). I have put the code below

<?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
*/   ?>

<?endif;?>   

<?endif;?>

<?php $layer->setCurrentCategory($_current_category); ?> 

<?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 end;?>

What would i have to change to get to to show the sub categories of All categories??

Thanks in advance

Best Answer

I found the answer on another wiki- http://www.magentocommerce.com/wiki/4_-_themes_and_template_customization/navigation/how_to_create_a_vertical_left_hand_menu

I just change my .phtml file to one called home_nav.phtml and added this code.

<div class="vertical-nav-container box base-mini">
<div class="vertical-nav">

<!--<div class="head">-->

<!--<h4>-->
<div class="block block-cart">
    <div class="block-title">
        <strong><span><?php echo $this->__('Categories') ?></span></strong>
</div><!--End block block-cart-->


<div class="block-content">

<!--</h4>-->
<!--</div>--><!--End Of head-->

<h4 class="no-display">
<?php echo $this->__('Category Navigation:') ?></h4>
<ul id="nav_vert">
<?php foreach ($this->getStoreCategories() as $_category): ?>
<?php echo $this->drawItem($_category) ?>
<?php endforeach ?> </ul>
</div>
</div><!--End Of vertical-nav-->

<?php echo $this->getChildHtml('topLeftLinks') ?>

</div></div><!--End Of vertical-nav-container box base-mini-->

Then displayed the code in a block using

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

a more detailed answer can be found on the wiki link at the top

Hopefully this can help soemone as i struggled with this for some time.

Related Topic