使用递归函数从 Magento 打印类别的嵌套列表
因此,我在 /[my-theme-name]/template/catalog/navigation/left.phtml 中有以下代码作为概念证明:
<?php
$Mage_Catalog_Block_Navigation = new Mage_Catalog_Block_Navigation();
$categories = $Mage_Catalog_Block_Navigation->getStoreCategories();
function render_flat_nav($categories) {
$html = '<ul>';
foreach($categories as $category) {
$html .= '<li><a href="' . $category->getCategoryUrl($cat) . '">' .
$category->getName() . "</a>\n";
if($category->hasChildren()) {
$children = $category->getChildren();
$html .= render_flat_nav($children);
}
$html .= '</li>';
}
return $html . '</ul>';
}
echo render_flat_nav($categories); ?>
它非常适合 0 级和 1 级类别,但任何嵌套更深的类别都适用从未打印出来。
因此 $category->getChildren()
无法完全返回我期望的结果。是否有一个我可以调用的方法可以与我的递归函数一起使用?
So I have the following code in /[my-theme-name]/template/catalog/navigation/left.phtml as a proof of concept:
<?php
$Mage_Catalog_Block_Navigation = new Mage_Catalog_Block_Navigation();
$categories = $Mage_Catalog_Block_Navigation->getStoreCategories();
function render_flat_nav($categories) {
$html = '<ul>';
foreach($categories as $category) {
$html .= '<li><a href="' . $category->getCategoryUrl($cat) . '">' .
$category->getName() . "</a>\n";
if($category->hasChildren()) {
$children = $category->getChildren();
$html .= render_flat_nav($children);
}
$html .= '</li>';
}
return $html . '</ul>';
}
echo render_flat_nav($categories); ?>
It works great for level 0 and level 1 categories but any categories that are more deeply nested are never printed out.
So $category->getChildren()
can't quite be returning what I expect it to. Is there a method I can call that will work with my recursive function?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我已经找到了问题的答案,但可能不是最佳答案:
I have found an answer to the problem, but it could be sub-optimal:
感谢以上内容,我设法让它适用于特定类别 id,并且也适用于平面类别。
Thanks to the above i managed to get this to work for a specific category id and this works with flat categories aswell.