我正在尝试在我的每个 Magento 页面的标题中创建一个下拉菜单。我想列出所有类别、子类别以及每个类别的关联图像。通过从 Internet 上抓取和组合代码,我想出了一个几乎可行的解决方案,只有子类别的 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>