使用递归函数从 Magento 打印类别的嵌套列表

发布于 2024-12-01 16:11:49 字数 925 浏览 0 评论 0原文

因此,我在 /[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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

灯角 2024-12-08 16:11:49

我已经找到了问题的答案,但可能不是最佳答案:

<?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); ?>

I have found an answer to the problem, but it could be sub-optimal:

<?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); ?>
$_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,并且也适用于平面类别。

$_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);

Thanks to the above i managed to get this to work for a specific category id and this works with flat categories aswell.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文