按名称排序类别集合

时间:2013-04-08 08:36:01

标签: magento magento-1.7

我在菜单中有自定义链接,在下拉列表中显示类别。 为此,我在catalog/navigation->mainmenu.phtml中创建了一个文件 (自定义文件) 现在,我希望在按名称排序之后显示类别。请帮助我如何按名称对类别集合进行排序。我已经在admin中设置了类别顺序。但在前端它显示未分类 代码是: -

<?php $defaultcategory= Mage::app()->getStore()->getRootCategoryId();?>
  <?php $mainchildren = Mage::getModel('catalog/category')->getCategories($defaultcategory);?>
   <ul>
              <?php foreach ($mainchildren as $subcategory) : ?> <?php // 2 level ?>
                <?php if($subcategory->getIsActive()):?>
                 <li id="show_subcat" class="<?php if($i==1): echo 'first'; endif; ?>" >
                      <?php  $childid=$subcategory->getId();?> 
                     <?php $subchild = Mage::getModel('catalog/category')->getCategories($childid);?>
                      <?php foreach ($subchild as $subchildcategory) : ?> 
                         <?php $path=$subchildcategory->getRequestPath()?>
                         <?php break;?>
                     <?php endforeach ?>
                     <a  href="<?php echo  $this->getUrl().$path; ?>">
                       <?php echo $name= $subcategory->getName().$subcategory->getEnable() ?>
                     </a>
                 </li>
                 <?php endif;?>
              <?php endforeach; ?>
            </ul>

2 个答案:

答案 0 :(得分:1)

您可以尝试:

children = Mage::getModel('catalog/category')->getCategories($defaultcategory)
                                             ->addAttributeToSort('name', 'ASC');

或:

children = Mage::getModel('catalog/category')->getCategories($defaultcategory)
                                             ->setOrder('name','ASC);

答案 1 :(得分:0)

$categories = Mage::helper('catalog/category');
$collection = $categories->getStoreCategories(false,true,false);
foreach($collection as $_category)
 {
    //Do something
    echo $_category->getName();
  }

使用此代码获取集合,并点击此链接以获取更多详细信息In magento - same code for getting all categories running well with sorted in localhost but not in web server