Magento - 列出phtml文件中的类别,子类别和图像

时间:2013-01-28 18:36:59

标签: image list magento url categories

我正在尝试创建一个位于每个Magento页面标题中的下拉菜单。我想列出所有类别,子类别和相关图像。从抓取和组合来自互联网的代码,我提出了一个几乎可行的解决方案,只有子类别的URL返回未定义,我不知道为什么。有任何想法吗?这是我的代码:

<?php
    $cats = Mage::getModel('catalog/category') -> load(2) -> getChildren();
    $catIds = explode(',', $cats);
    $categories = array();
    foreach ($catIds as $catId) {
        $category = Mage::getModel('catalog/category') -> load($catId);
        $categories[$category -> getName()] = array('name' => $category -> getName(), 'url' => $category -> getUrl(), 'img' => $category -> getImageUrl(), 'subcategories' => Mage::getModel('catalog/category') -> getCategories($catId));
    }
    ksort($categories, SORT_STRING);
?>
<ul>
    <?php foreach($categories as $name => $data): ?>
        <li><?php echo $data['name']; ?>
            <a href="<?php echo $data['url']; ?>" title="<?php echo $name; ?>">
                <img class="cat-image" src="<?php echo $data['img']; ?>" />
            </a>
            <ul>
            <?php
                foreach ($data['subcategories'] as $subcategory) {
                    echo "<a href='" . $subcategory -> getUrl() . "'><li>" . $subcategory -> getName() . "</li></a>";
                }
            ?>
            </ul>
        </li>
    <?php endforeach; ?>
</ul>

1 个答案:

答案 0 :(得分:1)

尝试更改

 'subcategories' => Mage::getModel('catalog/category') -> getCategories($catId)

  'subcategories' => $category->getChildrenCategories()

请参阅How To Get Sub Categories in Magento ?`