I need to automatically list the left nav alphabetically.
I have this code, but the problem is it only does it for category number 4 and I need it to do it for the current category. Any ideas how to get it to load the current category and not a specific number?
<?php
$cats = Mage::getModel('catalog/category')->load(4)->getChildren();
$catIds = explode(',',$cats);
$categories = array();
foreach($catIds as $catId) {
$category = Mage::getModel('catalog/category')->load($catId);
$categories[$category->getName()] = $category->getUrl();
}
ksort($categories, SORT_STRING);
?>
<ul>
<?php foreach($categories as $name => $url): ?>
<li>
<a href="<?php echo $url; ?>"><?php echo $name; ?></a>
</li>
<?php endforeach; ?>
</ul>
Many thanks,
Kirsty
<?php
if(Mage::registry('current_category')) {
$cats = Mage::registry('current_category')->getChildren();
$catIds = explode(',',$cats);
$categories = array();
foreach($catIds as $catId) {
$category = Mage::getModel('catalog/category')->load($catId);
$categories[$category->getName()] = $category->getUrl();
}
ksort($categories, SORT_STRING);
?>
<ul>
<?php foreach($categories as $name => $url): ?>
<li>
<a href="<?php echo $url; ?>"><?php echo $name; ?></a>
</li>
<?php endforeach; ?>
</ul>
<?php } ?>
For getting current category used below code -'Mage::registry('current_category')->getId()
'
if(Mage::registry('current_category'))
{
$cat=Mage::registry('current_category')->getId();
/* used for category level*/
}
else{
$cat=2; // 2 is root category id
/* used for serch level*/
}
$cats = Mage::getModel('catalog/category')->load($cat)->getChildren();