How to see all category and subcategory menu in Magento
By default there is a top.phtml file in magento which show all category and subcategory .To modify menu display style it needs to modify in core file whcih will create problem to Update magento in future,So without touching the core file we can access all category and subcategory in magento.
For that we will write the code in top.phtml file
<?php foreach ($this->getStoreCategories() as $_category): ?>
<li>
<a href="<?php echo $this->getCategoryUrl($_category) ?>"><?php echo $this->htmlEscape($_category->getName()) ?></a>
<?php
$_catid=$_category->getId();
$category = Mage::getModel('catalog/category')->load($_catid);
$subcategory = $category->getAllChildren(true);
array_shift($subcategory);
if($subcategory!=null)
{?>
<ul>
<?php
foreach ($subcategory as $sub)
{
$sub1 = Mage::getModel('catalog/category')->load( $sub);
?>
<li><a href="<?php echo $sub1->getUrl();?>"><span>
<?php echo $sub1->getName(); ?>
</span></a></li>
<?php } ?>
</ul>
<?php }?>
</li>
<?php endforeach ?>
For that we will write the code in top.phtml file
<?php foreach ($this->getStoreCategories() as $_category): ?>
<li>
<a href="<?php echo $this->getCategoryUrl($_category) ?>"><?php echo $this->htmlEscape($_category->getName()) ?></a>
<?php
$_catid=$_category->getId();
$category = Mage::getModel('catalog/category')->load($_catid);
$subcategory = $category->getAllChildren(true);
array_shift($subcategory);
if($subcategory!=null)
{?>
<ul>
<?php
foreach ($subcategory as $sub)
{
$sub1 = Mage::getModel('catalog/category')->load( $sub);
?>
<li><a href="<?php echo $sub1->getUrl();?>"><span>
<?php echo $sub1->getName(); ?>
</span></a></li>
<?php } ?>
</ul>
<?php }?>
</li>
<?php endforeach ?>
Hello, this is what I had been looking for. Thank you!
ReplyDeleteI have one question. Would you know how to recognize multiple sub-categories? In my case, there are main category, sub-category1, and sub-category2.
I'd like to add different css classes on sub-category1 and sub-category2.
I really appreciate if you let me share your idea. Thank you.
I'm sorry, I ment my subcategories are in hierarchical. Main category is on the top, subcategory1 is on the middle, and subcategory2 is on the bottom.
ReplyDeleteThanks for posting this! Is there a way I can apply a class to the active category and sub category links?
ReplyDeleteYes Caleb, You have to find for the current Url then check it with Url of the category, If it's true then echo "class='active'";
ReplyDelete