Reputation: 3
I am trying to list two sets of subcategories on my home page with the following code. The problem is that the second listing is exactly the same as the first. It doesn't seem to recognise the second category id number. any ideas?
<?php
$children = Mage::getModel('catalog/category')->getCategories(3);
foreach ($children as $category):
echo '<li><a href="' . $category->getUrl() . '">' . $category->getName() . '</a></li>';
endforeach;
?>
<?php
$children = Mage::getModel('catalog/category')->getCategories(4);
foreach ($children as $category):
echo '<li><a href="' . $category->getUrl() . '">' . $category->getName() . '</a></li>';
endforeach;
?>
Upvotes: 0
Views: 549
Reputation: 60
If you need just Url and Name of Subcategories, there is no need to load them as it can decrease site performance
Try,
$parentCategory = Mage::getModel('catalog/category')->load(15);
$subCategories = $parentCategory->getChildrenCategories();
foreach($subCategories as $curCategory){
echo '<li><a href="' . $curCategory->getUrl() . '">' . $curCategory->getName() . '</a></li>';
}
Here's you are using category ids, but i would prefer url_key instead, as there are chances for mismatch between category ids of development and live sites.
Try,
$parentCategory = Mage::getModel('catalog/category')->loadByAttribute('url_key', 'computers');
$subCategories = $parentCategory->getChildrenCategories();
foreach($subCategories as $curCategory){
echo '<li><a href="' . $curCategory->getUrl() . '">' . $curCategory->getName() . '</a></li>';
}
Upvotes: 1
Reputation:
Try this
<?php
$_helper = Mage::helper('catalog/category');
$_category = Mage::getModel('catalog/category')->load(3);
$_subcategories = $_category->getChildrenCategories();
foreach($_subcategories as $_category){
$category = Mage::getModel('catalog/category')->load($_category->getId());
echo '<li><a href="' . $category->getUrl() . '">' . $category->getName() . '</a></li>';
}
?>
<?php
$_helper = Mage::helper('catalog/category');
$_category = Mage::getModel('catalog/category')->load(4);
$_subcategories = $_category->getChildrenCategories();
foreach($_subcategories as $_category){
$category = Mage::getModel('catalog/category')->load($_category->getId());
echo '<li><a href="' . $category->getUrl() . '">' . $category->getName() . '</a></li>';
}
?>
This code is not tested by me. Thanks,
Upvotes: 2