Php 使用递归函数从Magento打印嵌套的类别列表
因此,我在/[my theme name]/template/catalog/navigation/left.phtml中有以下代码作为概念证明:Php 使用递归函数从Magento打印嵌套的类别列表,php,magento,Php,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(
<?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); ?>
我已经找到了问题的答案,但可能是次优的:
<?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); ?>
$\u category=Mage::getModel('catalog/category')->加载(257);
$\类别=$\类别
->getCollection()
->addAttributeToSelect(数组('name'、'image'、'description'))
->addIdFilter($_category->getChildren());
函数渲染平面导航($categories){
$html=“”;
foreach($categories作为$category){
$html.='- \n”;
如果($category->hasChildren()){
$children=Mage::getModel('catalog/category')->getCategories($category->entity_id);
$html.=渲染平面导航($children);
}
$html.='
';
}
返回$html。“
”;
}
回声渲染平面导航($\类别);
由于上述原因,我成功地将其用于特定的类别id,这也适用于平面类别。什么是$category->getChildren()
返回的?它返回一个类集合,但我假设它与getStoreCategories()返回的集合在某些方面有所不同
它不返回类别的子级吗?我不知道magento,请检查文档。也许您可以使用Mage_Catalog_Block_Navigation#RenderCategoriesMinuHTML(请参见)?为什么需要额外的功能?
$_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);