2013-04-08 60 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

相關問題