[英]Magento - list categories, subcategories, and images in phtml file
I'm trying to create a dropdown menu located in header of each of my Magento pages. 我正在尝试在每个Magento页面的页眉中创建一个下拉菜单。 I want to list all of the categories, the subcategories, and the associated images with each.
我想列出所有类别,子类别以及与之相关的图像。 From scraping and combining code from the Internet, I've come up with a solution that almost works, only the URLs for the subcategories are returning as undefined, and I don't know why.
通过从Internet抓取和合并代码,我提出了一个几乎可行的解决方案,只有子类别的URL返回的是未定义的,而且我不知道为什么。 Any ideas?
有任何想法吗? Here's my code:
这是我的代码:
<?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>
Try changing 尝试改变
'subcategories' => Mage::getModel('catalog/category') -> getCategories($catId)
to 至
'subcategories' => $category->getChildrenCategories()
See How To Get Sub Categories in Magento ?` 请参阅如何在Magento中获取子类别?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.