繁体   English   中英

Magento在模板中排序类别

[英]Magento sort Categories in template

我正在寻找一种方法来对导航中类别的前端显示进行排序。

这是我导航的代码:

<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的面向对象编程来说这似乎是显而易见的)。 我试图找到一个对象的解决方案,但失败了,现在我有点卡住了。

谢谢你的帮助。

$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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM