2012-03-07 69 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