2016-11-23 47 views
0

我正在使用下面的代碼獲取類別'9'的所有子類別。
該代碼很好,但它按id或創建日期排序子類別,我需要按字母順序排序子類別。按字母順序獲取Magento子類別

<?php 
$categories = Mage::getModel('catalog/category')->getCollection()->addAttributeToSelect('*')->addAttributeToFilter('parent_id', 9); 

foreach ($categories as $cat) { ?> 
    <?php 
     $entity_id = $cat->getId(); 
     $name = $cat->getName(); 
     $url_key = $cat->getUrlKey(); 
     $url_path = $cat->getUrlPath(); 
     $skin_url = $cat->getImageUrl(); 
    ?> 
    <div> 
     <a href="<?php echo $url_path ?>"> 
      <?php 
       echo '<img style="width: 100%;" src="'.$skin_url.'" />'; 
      ?> 

      <div class="brand-border-top"></div> 
      <div class="brand-border-bottom"></div> 
      <div class="brand-overlay"> 
       <?php echo $name = $cat->getName(); ?> 
      </div> 
     </a> 
    </div> 
<?php } ?> 

回答

1

使用

->addAttributeToSort('name','ASC'); 

在你的情況

$categories = Mage::getModel('catalog/category')->getCollection()->addAttributeToSelect('*')->addAttributeToFilter('parent_id', 9)->addAttributeToSort('name','ASC'); 
+0

它是如此簡單...謝謝:) – filippo90