2013-04-08 99 views
0

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

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

你的意思是“排序”? – Djouuuuh 2013-04-08 08:44:40

回答

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

+0

它不工作.... – 2013-04-08 11:40:25

+0

一些变化是: - $ collection = $ categories-> getStoreCategories(true,true,false);.它将获取所有类别,因此根据需要添加条件。 – 2013-04-08 11:48:24

1

你可以试试:

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

或:

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

在这两种情况下,它显示致命错误 – 2013-04-08 09:27:17

+0

如何通过“$ defaultcategory-> getChildrenCategories()”替换“Mage :: getModel('catalog/category') - > getCategories($ defaultcategory)”? – Djouuuuh 2013-04-08 09:36:07

+0

它也显示致命的errot – 2013-04-08 09:52:18

相关问题