想要在magento中显示子类别


Want to show sub category in magento

我想在我的magento静态块中显示子类别。

例如,该页面上有"女性"类别。我想展示女性的所有子类别,以及这个子类别。

女性页面的结构将是

Category 1 -> sub 1 -> Sub 1

我已经实现了一些代码,但它没有显示子类别:

<?php 
//If there are sub categories
$categories = $this->getCurrentChildCategories();
$categoriescount = $this->getCurrentChildCategories()->count();
if ($categoriescount > 0): 
?>
<div class="sub-category-container">    
    <?php 
    //Loop through categories
    foreach ($categories as $category):
    ?>
    <div class="sub-category">
        <a href="<?php echo $this->getCategoryUrl($category)?>" class="cat-image">
        <?php 
        // If there is a thumbnail set for the category - Display it
        if($imgUrl = Mage::getModel('catalog/category')->load($category->getId())->getThumbnail()):?>
        <img src="<?php echo $this->getBaseUrl()."media/catalog/category/".$imgUrl ?>" width="220" height="110" alt="<?php echo $this->htmlEscape($category->getName()) ?>" />
        <?php endif; ?>
        </a>
        <div class="inner-sub-category">
            <a href="<?php echo $this->getCategoryUrl($category)?>" class="sub-link"><?php echo $category->getName()?></a>
            <a href="<?php echo $this->getCategoryUrl($category)?>" class="btn"><span>View All</span></a>
        </div>
    </div>
    <?php endforeach; ?>
</div>
<?php else:?>
<p>No Sub Categories</p>
<?php endif; ?>

输出上述代码http://prntscr.com/6wcfov

查看Categories模型中的getCategories()方法。

$subcategories = Mage::getModel('catalog/category')->getCategories($parentCategory->getId());
foreach ($subcategories as $subcategory) {
    ...do things
}

您可以递归地执行此操作,以获取父类别中的所有子类别。

显示当前类别的子类别

    $loadCategory = Mage::getModel('catalog/category')->load($currentCat->getId());
$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>';
    }
}