2012-03-07 54 views
0

我正在尋找一種方法來排序我的導航中類別的前端顯示。Magento排序模板中的類別

這是我的導航代碼:

<div id="menu-accordion" class="accordion">  
    <?php 

    foreach ($this->getStoreCategories() as $_category): ?> 
    <?php $open = $this->isCategoryActive($_category) && $_category->hasChildren(); ?> 
    <h3 class="accordion-toggle"><a href="#"><?php print $_category->getName();?></a></h3> 
     <div class="accordion-content"> 
       <ul> 
       <?php foreach ($_category->getChildren() as $child): ?> 
        <li> 
         <span class="ui-icon ui-icon-triangle-1-e vMenuIconFloat"></span> 
          <a href="<?php print $this->getCategoryUrl($child); ?>"><?php print $child->getName();?></a> 
        </li> 
       <?php endforeach; ?> 
       </ul> 
      </div> 
    <?php endforeach ?> 
</div> 

我試着用asort()排序$this->getStoreCategories(),但它解決了一個錯誤500,所以我想這不是一個數組,而是一個對象(這似乎對於magento的面向對象編程來說是顯而易見的)。我試圖找到對象的解決方案,但失敗了,現在我有點卡住了。

感謝您的幫助。

回答

2

$this->getStoreCategories()的調用不返回數組。但是你可以建立自己的數組,並使用數組的鍵作爲元素進行排序(假設你想要的類別名稱排序):

foreach ($this->getStoreCategories() as $_category) 
{ 
    $_categories[$_category->getName()] = $_category; 
} 

ksort($_categories); 

現在改爲迭代$this->getStoreCategories()你遍歷$ _categories數組。所以你的代碼看起來像這樣:

<div id="menu-accordion" class="accordion">  
    <?php 

    $_categories = array(); 
    foreach ($this->getStoreCategories() as $_category) 
    { 
     $_categories[$_category->getName()] = $_category; 
    } 
    ksort($_categories); 

    foreach ($_categories as $_category): ?> 
    <?php $open = $this->isCategoryActive($_category) && $_category->hasChildren(); ?> 
    <h3 class="accordion-toggle"><a href="#"><?php print $_category->getName();?></a></h3> 
     <div class="accordion-content"> 
       <ul> 
       <?php foreach ($_category->getChildren() as $child): ?> 
        <li> 
         <span class="ui-icon ui-icon-triangle-1-e vMenuIconFloat"></span> 
          <a href="<?php print $this->getCategoryUrl($child); ?>"><?php print $child->getName();?></a> 
        </li> 
       <?php endforeach; ?> 
       </ul> 
      </div> 
    <?php endforeach ?> 
</div> 
+0

好吧,主要類別的作品相當不錯,但子類別不排序。 – Maddis 2012-03-08 14:21:49

+0

我對孩子類別做了同樣的事情,現在它幾乎完美了,我只需要找到一種緩存方法。謝謝 – Maddis 2012-03-08 23:01:17