从类别ID显示特定的Magento类别

时间:2012-05-14 14:12:20

标签: magento magento-1.6

到目前为止,我还没有在网上找到任何已经满足我想要实现的目标的东西。我只是想在列表中调用特定类别,但我希望能够按ID定义哪些类别,例如,我希望能够像下面这样调用它们: -

{{block type="catalog/navigation" name="catalog.category" template="developer/extension/script.phtml" ids="3,6,17,143,57"}}

我已经根据父类别ID显示了各个地方的子类别列表,但是在有数百个子类别的情况下,显示所有子类别并不总是切实可行,所以我想知道如果可以调整现有脚本以仅包含上述特定类别?

<?php
//gets all sub categories of parent category 'cat-id-4'
   $cats = Mage::getModel('catalog/category')->load(4)->getChildren();
   $catIds = explode(',',$cats);

   $categories = array();
foreach($catIds as $catId) {
    $category = Mage::getModel('catalog/category')->load($catId); 
    $categories[$category->getName()] = array(
        'url' => $category->getUrl(),
        'img' => $category->getImageUrl()
    );
}

ksort($categories, SORT_STRING);
?>
<ul>
    <?php if($category->getIsActive()): ?>
    <?php foreach($categories as $name => $data): ?>
        <li>
            <a href="<?php echo $data['url']; ?>" title="<?php echo     $name; ?>" alt="<?php echo $name; ?>"><?php echo $name; ?></a>
        </li>   
    <?php endforeach; ?>
    <?php endif; ?>
</ul>

如果有人可以建议我怎么可能实现这一点,那将是非常棒的 - 提前谢谢。

1 个答案:

答案 0 :(得分:2)

这应该与您给定的CMS块代码一起使用:

<?php
$catIds = explode(',', $this->getIds()); //<-- ONLY CHANGE MADE

$categories = array();
foreach($catIds as $catId) {
    $category = Mage::getModel('catalog/category')->load($catId); 
    $categories[$category->getName()] = array(
        'url' => $category->getUrl(),
        'img' => $category->getImageUrl()
    );
}

ksort($categories, SORT_STRING);
?>
<ul>
    <?php if($category->getIsActive()): ?>
    <?php foreach($categories as $name => $data): ?>
        <li>
            <a href="<?php echo $data['url']; ?>" title="<?php echo $name; ?>" alt="<?php echo $name; ?>"><?php echo $name; ?></a>
        </li>   
    <?php endforeach; ?>
    <?php endif; ?>
</ul>