按字母顺序排列的类别(magento)


Categories in alphabetical order (magento)

我试图在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);
                }
            ?>

我错过了什么吗?

试试这个代码:

    <?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>

我觉得你应该试试这个。

$allCategories = Mage::getResourceModel('catalog/category_collection')
              ->addAttributeToSelect('name')
              ->addAttributeToSort('name','ASC');

试试这个:

$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');

这将使当前商店中的所有活动类别按名称排序,忽略级别。