我正在一个网站上工作,我正在显示与当前类别相关的所有子类别的列表。下面的代码可以正常工作,但我想改变子类别列表的排序方式。目前,它按类别ID排序。我希望它以Magento用户在管理员中放置类别的任何顺序显示(他们可以通过拖放来更改类别顺序)。感谢任何帮助!
<?php
$currentCat = Mage::registry('current_category');
if ( $currentCat->getParentId() == Mage::app()->getStore()->getRootCategoryId() )
{
// current category is a toplevel category
$loadCategory = $currentCat;
}
else
{
// current category is a sub-(or subsub-, etc...)category of a toplevel category
// load the parent category of the current category
$loadCategory = Mage::getModel('catalog/category')->load($currentCat->getParentId());
}
$subCategories = explode(',', $loadCategory->getChildren());
foreach ( $subCategories as $subCategoryId )
{
$cat = Mage::getModel('catalog/category')->load($subCategoryId);
if($cat->getIsActive())
{
echo '<a href="'.$cat->getURL().'">'.$cat->getName().'</a>';
}
}
?>
答案 0 :(得分:6)
尝试调用getChildrenCategories,这将考虑每个类别的位置:
$loadCategory->getChildrenCategories()
<强> EDITED 强>
与getChildren不同,它返回所有类别ID的字符串,返回Mage_Catalog_Model_Category数组,因此需要更改代码以将其考虑在内。
从上面的代码段开始,以下更改应该有效。请注意对getChildrenCategories()的调用以及foreach循环中的更改,因为每个项都应该是一个类别对象。
<?php
$currentCat = Mage::registry('current_category');
if ( $currentCat->getParentId() == Mage::app()->getStore()->getRootCategoryId() )
{
// current category is a toplevel category
$loadCategory = $currentCat;
}
else
{
// current category is a sub-(or subsub-, etc...)category of a toplevel category
// load the parent category of the current category
$loadCategory = Mage::getModel('catalog/category')->load($currentCat->getParentId());
}
$subCategories = $loadCategory->getChildrenCategories();
foreach ( $subCategories as $subCategory )
{
if($subCategory->getIsActive())
{
echo '<a href="'.$subCategory->getURL().'">'.$subCategory->getName().'</a>';
}
}
?>