我正在尝试使用以下代码在我的主页上列出两组子类别。问题是第二个列表与第一个列表完全相同。它似乎没有识别第二类ID号。任何想法?
<?php
$children = Mage::getModel('catalog/category')->getCategories(3);
foreach ($children as $category):
echo '<li><a href="' . $category->getUrl() . '">' . $category->getName() . '</a></li>';
endforeach;
?>
<?php
$children = Mage::getModel('catalog/category')->getCategories(4);
foreach ($children as $category):
echo '<li><a href="' . $category->getUrl() . '">' . $category->getName() . '</a></li>';
endforeach;
?>
答案 0 :(得分:2)
试试这个
<?php
$_helper = Mage::helper('catalog/category');
$_category = Mage::getModel('catalog/category')->load(3);
$_subcategories = $_category->getChildrenCategories();
foreach($_subcategories as $_category){
$category = Mage::getModel('catalog/category')->load($_category->getId());
echo '<li><a href="' . $category->getUrl() . '">' . $category->getName() . '</a></li>';
}
?>
<?php
$_helper = Mage::helper('catalog/category');
$_category = Mage::getModel('catalog/category')->load(4);
$_subcategories = $_category->getChildrenCategories();
foreach($_subcategories as $_category){
$category = Mage::getModel('catalog/category')->load($_category->getId());
echo '<li><a href="' . $category->getUrl() . '">' . $category->getName() . '</a></li>';
}
?>
此代码未经我测试。 谢谢,
答案 1 :(得分:1)
如果您只需要Url和子类别名称,则无需加载它们,因为它会降低网站性能
尝试,
$parentCategory = Mage::getModel('catalog/category')->load(15);
$subCategories = $parentCategory->getChildrenCategories();
foreach($subCategories as $curCategory){
echo '<li><a href="' . $curCategory->getUrl() . '">' . $curCategory->getName() . '</a></li>';
}
这里是您使用类别ID,但我更喜欢url_key,因为开发类别ID和实时网站之间存在不匹配的可能性。
尝试,
$parentCategory = Mage::getModel('catalog/category')->loadByAttribute('url_key', 'computers');
$subCategories = $parentCategory->getChildrenCategories();
foreach($subCategories as $curCategory){
echo '<li><a href="' . $curCategory->getUrl() . '">' . $curCategory->getName() . '</a></li>';
}