如何在Magento中按字母顺序对类别列表数组进行排序 [英] How to sort a category list array alphabetically in Magento

查看:92
本文介绍了如何在Magento中按字母顺序对类别列表数组进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Magento中,我使用以下代码创建了一个phtml模板文件.我从本教程中获得了 .我和其他人想知道如何按字母顺序对这个类别列表进行排序.代码的第一行创建具有类别ID的数组.再往下,我们可以使用foreach部分中的ID获取类别名称.但是要按名称排序,我们需要在foreach之前的数组中获取名称,然后再按名称排序.怎么样?

In Magento, I've created a phtml template file with the code below. I got this from this tutorial. Me and others are wondering how to sort this category list alphabetically. The first lines of code create an array with Category IDs. Further down, we can get the Category Name using the ID within the foreach section. But to sort by Name, we need to get the Names in an array before the foreach and then sort by name. How?

<?php
$cats = Mage::getModel('catalog/category')->load(319)->getChildren();
$catIds = explode(',',$cats);
?>
<ul>
<?php foreach($catIds as $catId): ?>
    <li>
        <?php
            $category = Mage::getModel('catalog/category')->load($catId);
            echo '<a href="' . $category->getUrl() . '">';
            echo $category->getName() . '</a>';
        ?>
    </li>
<?php endforeach; ?>
</ul>

注意:319是我要为其列出子类别的父类别的类别ID.另外,我也不把这放在类别页面模板上.我正在作为一个块插入CMS页面(该部分已经在工作).

推荐答案

您可以先构建类别名称列表.

You could build a list of category names first.

<?php
$cats = Mage::getModel('catalog/category')->load(319)->getChildren();
$catIds = explode(',',$cats);

$categories = array();
foreach($catIds as $catId) {
       $category = Mage::getModel('catalog/category')->load($catId); 
       $categories[$category->getName()] = $category->getUrl();
}

ksort($categories, SORT_STRING);
?>

<ul>
<?php foreach($categories as $name => $url): ?>
    <li>
    <a href="<?php echo $url; ?>"><?php echo $name; ?></a>
    </li>
<?php endforeach; ?>
</ul>

我写这个答案的时候,对Magento并不了解太多,只是想快速找到可行的方法. 安东的答案更好,而且更多的是Magentic(?)

I wrote this answer without knowing too much about Magento and just wanting something quickly that worked. Anton's answer is better and more Magentic(?)

这篇关于如何在Magento中按字母顺序对类别列表数组进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆