所以我在/[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()
无法完全按照我的预期返回。有没有我可以调用的方法可以使用我的递归函数?
答案 0 :(得分:1)
我找到了问题的答案,但可能不是最佳的:
<?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 = Mage::getModel('catalog/category')->getCategories($category->entity_id);
$html .= render_flat_nav($children);
}
$html .= '</li>';
}
return $html . '</ul>';
}
echo render_flat_nav($categories); ?>
答案 1 :(得分:0)
$_category = Mage::getModel('catalog/category')->load(257);
$_categories = $_category
->getCollection()
->addAttributeToSelect(array('name', 'image', 'description'))
->addIdFilter($_category->getChildren());
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 = Mage::getModel('catalog/category')->getCategories($category->entity_id);
$html .= render_flat_nav($children);
}
$html .= '</li>';
}
return $html . '</ul>';
}
echo render_flat_nav($_categories);
由于上述原因,我设法让这个用于特定的类别ID,这也适用于平面类别。