2016-01-07 106 views
2

我使用以下块在我的Magento CMS产品网站:Magento 1.9:如何获取特定类别的子类别?

{{block type="catalog/product_list" name="catalog_list" category_id="1420" template="catalog/product/listStart.phtml"}} 

如何为(在这种情况下编号1420)在块中指定我能不能得到CATEGORY_ID的所有子类别的输出。

到目前为止,我有以下代码:

<?php 
$_category = $this->getCurrentCategory(); 
$collection = Mage::getModel('catalog/category')->getCategories($_category->entity_id); 
$helper = Mage::helper('catalog/category'); 
?> 
<div class="category-products"> 
<div id="carousel"> 
    <ul class="products-in-row"> 
     <?php $i=0; foreach ($collection as $cat): ?> 
      <li class="item"> 
       <?php echo $cat->getName();?> 
      </li> 
     <?php endforeach ?> 
    </ul> 
</div> 

我只得到了主要类别的所有子类别。

+0

的可能的复制[Magento的 - 得到一个父类和所有子子类别](http://stackoverflow.com/questions/5564647/magento-get-a-parent-category-and-all-sub-sub-categories) –

+0

@DouglasRadburn - 不同的问题。就我所知,这个问题中的这个问题无法将从'category_id'中的值'1420'应用到集合中,而不是一般如何执行。对于您所链接的问题来说,这也是一个不同的场景。 –

回答

2

此代码可以帮助,如果你想获得每一个当前类的子类

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

另一种方式:

<?php 
    $categoryId = 10 ; // get current category id 
    $category = Mage::getModel('catalog/category')->load($categoryId); 
    $catList = explode(",", $category->getChildren()); 
    foreach($catList as $cat) 
    { 
    $subcategory = Mage::getSingleton('catalog/category')->load($cat); 
    echo $subcategory->getName(); 
    } 
?> 
+0

我在第二个例子中修正了一些拼写错误,它适用于我。我认为这会对我有帮助。谢谢。 –

+0

我认为这不考虑子类别的顺序... – Wouter

相关问题