我需要按字母顺序自动列出左侧导航。
我有这个代码,但问题是它只对类别4做了,我需要它来为当前类别做。有任何想法如何让它加载当前类别而不是特定的数字?
<?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>
非常感谢,
科斯蒂
答案 0 :(得分:0)
获取代码下方使用的当前类别 - “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();
答案 1 :(得分:0)
<?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 } ?>