Skip to content

Instantly share code, notes, and snippets.

@dannevang
Created June 18, 2013 16:00
Show Gist options
  • Save dannevang/5806597 to your computer and use it in GitHub Desktop.
Save dannevang/5806597 to your computer and use it in GitHub Desktop.
Magento. Get categories by ID and display subcategories if any. Will work directly from top.phtml
<?php $_helper = Mage::helper('catalog/category');
$children = Mage::getModel('catalog/category')->getCategories(CATETGORY_ID_HERE);
foreach ($children as $category):
$category = Mage::getModel('catalog/category')->load($category->getId());
echo '<li class="level1"><a href="' . $category->getUrl() . '">' . $category->getName() . '</a>';
$category = Mage::getModel('catalog/category')->load($category->getId());
$_subcategories = $category->getChildrenCategories();
if (count($_subcategories) > 0): ?>
<ul>
<?php foreach($_subcategories as $_subcategory): ?>
<li class="level2">
<a href="<?php echo $_helper->getCategoryUrl($_subcategory) ?>">
<?php echo $_subcategory->getName() ?>
</a>
</li>
<?php endforeach; ?>
</ul>
<?php endif;
echo '</li>';
endforeach;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment