我试图在magento中按字母顺序列出所有类别,但它不起作用。
<?php foreach ($this->getStoreCategories() as $_category): ?>
<?php
//arrange by Letter
foreach($_category->getChildren() as $_sub){
$letter = substr(strtolower($_sub->getName()),0,1);
$subCategories[$letter][0]['name'] = $letter; //0 index is the letter
$i = 1;
while(isset($subCategories[$letter][$i])){
$i++;
}
$subCategories[$letter][$i]['name'] = $_sub->getName();
$subCategories[$letter][$i]['url'] = $this->getCategoryUrl($_sub);
}
?>
我在这里错过了什么吗?
答案 0 :(得分:2)
试试这段代码:
<?php
$cats = Mage::getModel('catalog/category')->load('2')->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>
答案 1 :(得分:0)
我觉得你应该试试这个。
$allCategories = Mage::getResourceModel('catalog/category_collection')
->addAttributeToSelect('name')
->addAttributeToSort('name','ASC');
答案 2 :(得分:0)
试试这个:
$root = Mage::app()->getStore()->getRootCategoryId();
$categories = Mage::getModel('catalog/category')->getCollection()
->addAttributeToSelect('name')
->addPathsFilter('1/'.$root)
->addFieldToFilter('level', array('gt'=>2))
->addIsActiveFilter()
->addAttributeToSort('name', 'ASC');
这将使您按名称排序当前商店中的所有活动类别,忽略该级别。