想要在magento中显示子类别

时间:2015-04-10 13:10:53

标签: php magento categories

我想在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

2 个答案:

答案 0 :(得分:0)

查看“类别”模型中的getCategories()方法。

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

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

答案 1 :(得分:0)

  

显示当前类别的子类别

     

    $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>';
    }
}
相关问题