如何在 Magento 中按字母顺序对类别列表数组进行排序
在 Magento 中,我使用下面的代码创建了一个 phtml 模板文件。我从本教程中得到了这个。我和其他人想知道如何按字母顺序对这个类别列表进行排序。代码的第一行创建一个包含类别 ID 的数组。再往下,我们可以使用 foreach 部分中的 ID 获取类别名称。但要按名称排序,我们需要在 foreach 之前获取数组中的名称,然后按名称排序。如何?
<?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 页面中作为块插入(该部分已经在工作)。
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>
Note: 319 is the category id of the parent category for which I want to list subcategories. Also, I'm not putting this is a category page template. I'm inserting as a block in a CMS page (that part is already working).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以调用
,您将立即对整个集合进行排序,其余的只是对典型 Varien 集合的迭代。这是一个伪样本,我不知道parent_id是否是数据库中的实际字段名称,因此您可以在获得正确的结果之前检查一下。
关于 Magento 中集合的精彩阅读是作者:艾伦·斯托姆
You can call
and you'll get the whole bunch sorted right away, rest is just iteration over typical Varien collection. It's a pseudo sample and i don't know if the parent_id is the actual field name in db so you might check that out before you get the right results.
Great read about collections in Magento is written by Alan Storm
您可以先建立一个类别名称列表。
我写这个答案时对 Magento 不太了解,只是想快速找到一些有用的东西。 Anton 的答案更好,更 Magentic(?)
You could build a list of category names first.
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 1.4.1.1,这可以对子类别进行排序:
用于
获取 id 为 319 的类别的子类别
并在函数
getChildrenCategories()
下的第 582 行编辑code/core/Mage/catalog/Model/Resource/Eav/Mysql4/category.php
文件,将其更改为
希望这也适合你。
Hi I am using magento 1.4.1.1 and this worked to sort the child categories:
use
to get the children categories of category with id 319
and edit the file at
code/core/Mage/catalog/Model/Resource/Eav/Mysql4/category.php
at line 582 under the functiongetChildrenCategories()
to changeto
hope this works for you too.
在最新的 Magento (CE 1.7.0.2)+ 中有一种更简单的方法来做到这一点,
函数
getChildren()
位于...app/code/core/Mage/Catalog/Model /Category.php 第 817 行左右
有很多选项。希望它能为您节省一些时间!
There's a much easier way to do this in the latest Magento (CE 1.7.0.2)+
The function
getChildren()
resides at...app/code/core/Mage/Catalog/Model/Category.php around line 817
There are loads of options. Hope it saves you some time!
首先备份您的 topmenu.phtml,然后在新的 topmenu.phtml 文件中替换以下代码
First backup your topmenu.phtml then replace the following code in your new topmenu.phtml file