0

我正在尝试使用以下代码在我的主页上列出两组子类别。问题是第二个清单与第一个清单完全相同。它似乎无法识别第二个类别的 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;
?>
4

2 回答 2

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>';
}
?>

此代码未经我测试。谢谢,

于 2013-06-29T10:08:01.750 回答
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>';
}
于 2013-06-29T10:42:05.963 回答