如何获取特定父类别的子类别?

ati*_*tif 25 magento

我有一个主要类别(父类别),其id = 10.我想回应它的子类别.我怎样才能做到这一点?

clo*_*eek 66

$children = Mage::getModel('catalog/category')->getCategories(10);
foreach ($children as $category) {
    echo $category->getName();
}
Run Code Online (Sandbox Code Playgroud)

  • @shnozolla只使用`getUrl()`而不是`getName()`. (2认同)

Abh*_*rma 8

如果您想获得每个当前类别的子类别,此代码可能会有所帮助

    <?php 
    $layer = Mage::getSingleton('catalog/layer');
    $_category = $layer->getCurrentCategory();
    $currentCategoryId= $_category->getId();
    $children = Mage::getModel('catalog/category')->getCategories($currentCategoryId);
    foreach ($children as $category)
    {
          echo $category->getName(); // will return category name 
          echo $category->getRequestPath(); // will return category URL
    }
    ?>
Run Code Online (Sandbox Code Playgroud)


jos*_*tgv 7

其他方式:

$children = Mage::getModel('catalog/category')->load(10)->getChildrenCategories();
foreach ($children as $category):
    $category = Mage::getModel('catalog/category')->load($category->getId());
    echo '<li><a href="' . $category->getUrl() . '">' . $category->getName() . '</a></li>';
endforeach;
Run Code Online (Sandbox Code Playgroud)