0

我在正确的时间关闭我的 li 和 ul 有问题。

使用我们拥有的代码,我们可以在 magento 商店中检索特定类别的所有子项。

现在的问题是我想将所有孩子分成列表。所以我们可以让它们按类别 -> 子类别 -> 子子类别排序。我希望我的结构是;

<ul>
    <li>
        <a>Head Child</a>
        <ul>
            <li>
                <a>Sub child</a>
                <ul>
                    <li><a>Sub sub child</a></li>
                    <li><a>Sub sub child</a></li> 
                </ul>
            </li>
        </ul>
    </li>
    <li>
        <a>Head Child</a>
        <ul>
            <li>
                <a>Sub child</a>
                <ul>
                    <li><a>Sub sub child</a></li>
                    <li><a>Sub sub child</a></li> 
                </ul>
            </li>
        </ul>
    </li>
</ul>

我现在得到的输出是

<ul>
    <a title="View the products for the " href="#">Head child</a>
    <li class="sub_cat">
        <a title="View the products for the " href="#">Sub child</a>
    </li>
    <li class="sub_cat">
        <a title="View the products for the " href="#">Sub sub child</a>
    </li>
</ul>

这是我们的 php 代码;

<?php
$cat = Mage::getModel('catalog/category')->load(9);
$subcats = $cat->getChildren();

foreach(explode(',',$subcats) as $subCatid)
{
    $_category = Mage::getModel('catalog/category')->load($subCatid);
    if($_category->getIsActive()) {
        echo '<ul><a href="'.$_category->getURL().'" title="View the products for the "'.$_category->getName().'" category">'.$_category->getName().'</a>';
        $sub_cat = Mage::getModel('catalog/category')->load($_category->getId());
        $sub_subcats = $sub_cat->getChildren();
        foreach(explode(',',$sub_subcats) as $sub_subCatid)
        {
            $_sub_category = Mage::getModel('catalog/category')->load($sub_subCatid);
            if($_sub_category->getIsActive()) {
                echo '<li class="sub_cat"><a href="'.$_sub_category->getURL().'" title="View the products for the "'.$_sub_category->getName().'" category">'.$_sub_category->getName().'</a>';
                $sub_sub_cat = Mage::getModel('catalog/category')->load($sub_subCatid);
                $sub_sub_subcats = $sub_sub_cat->getChildren();

                foreach(explode(',',$sub_sub_subcats) as $sub_sub_subCatid)
                {
                    $_sub_sub_category = Mage::getModel('catalog/category')->load($sub_sub_subCatid);
                    if($_sub_sub_category->getIsActive()) {
                        echo '<li class="sub_cat"><a href="'.$_sub_sub_category->getURL().'" title="View the products for the "'.$_sub_sub_category->getName().'" category">'.$_sub_sub_category->getName().'</a>';
                    }
                }
            }
        }
        echo '</ul>';
    }
}

?>
4

1 回答 1

0

你应该考虑在这里使用递归函数

$cat =  Mage::getModel('catalog/category')->load(9);
$html = '';
$html = getSubCategoriesHTML($cat, $html);



function getSubCategoriesHTML($cat, $html) {

    $html .= '<a href="'.$cat->getURL().'" title="View the products for the "'.$cat->getName().'" category">'.$cat->getName().'</a>';
    $html .= '<ul>';

    $subcats = $cat->getChildren();
    foreach(explode(',',$subcats) as $subCatid) {
        $_category = Mage::getModel('catalog/category')->load($subCatid);
        if($_category->getIsActive()) {
            $html .= '<li>';
            $html .=  getSubCategoriesHTML($_category, $html);
            $html .= '</li>;
        }
    }

    $html .= '</ul>';
    return $html;
}

您可以在帮助程序或类别模型中添加此功能。
我根本没有测试过它,所以它可能无法正常工作。但我希望它可以帮助你。

于 2013-01-30T09:37:20.507 回答