4

到目前为止,我想我正在做这样的事情

<?php 
$parentId = $_category->getParentId();
$cats = Mage::getModel('catalog/category')->load($parentId)->getChildred();
$catIds = explode(',',$cats);
?>

<ul>
<?php foreach($catIds as $catId): ?>
    <li>
    <?php
        $category=Mage::getModel('catalog/category')->load($catId)
        echo $category->getName();
    ?>
</li>
<?php endforeach; ?>
</ul>

我试图在子类别中进行横向移动,而不必上一层。

让我知道这是否有意义。

4

2 回答 2

4

下面的代码将帮助您加载第二级(子)类别。您也可以通过Mage::app()->getStore()->getRootCategoryId();将 $_rootCatId 设置为动态的;.

$_rootCatId     = 2;//Root Category
$_rootCategory  = Mage::getModel('catalog/category')->load($_rootCatId);
$_catName       = $_rootCategory->getName();
if($_rootCategory->hasChildren()) 
{
    $_collection = Mage::getResourceModel('catalog/category_collection')
                    ->addAttributeToFilter('is_active', 1)
                    ->addAttributeToSort('position', 'asc')
                    ->joinUrlRewrite()
                    ->addIdFilter($_rootCategory->getChildren())
                    ->load();

    foreach($_collection AS $_sub)
    {
        $_subCat    = Mage::getModel('catalog/category')->load($_sub->getId());

        if($_subCat->hasChildren()) 
        {   
            echo '<li class="leve10 nav-'. $_subCat->getId() .'">';
                echo '<a id="subCatLink">';
                    echo '<span>'. $this->htmlEscape($_subCat->getName()) .'</span>
                    </a>
             </li>';
            echo '<ul id="subCatUl" class="no-display" style="padding:0px 13px;">';
            $__collection = Mage::getResourceModel('catalog/category_collection')
                    ->addAttributeToFilter('is_active', 1)
                    ->addAttributeToSort('position', 'asc')
                    ->joinUrlRewrite()
                    ->addIdFilter($_subCat->getChildren())
                    ->load();

            foreach($__collection AS $__sub)
            {
                $__subCat   = Mage::getModel('catalog/category')->load($__sub->getId());
                echo '<li class="leve20 nav-'. $__subCat->getId() .'">
                        <a href="'. $this->getCategoryUrl($__subCat) .'">
                         <span>'. $this->htmlEscape($__subCat->getName()) .'</span>
                        </a>
                     </li>';
            }
            echo '</ul>';
        }
        else
        {
            echo '<li class="leve10 nav-'. $_subCat->getId() .'">
                <a href="'. $this->getCategoryUrl($_subCat) .'">
                 <span>'. $this->htmlEscape($_subCat->getName()) .'</span>
                </a>
             </li>';
        }
    }
}
else
    echo 'No Categories Found...';
于 2012-10-24T15:47:42.760 回答
1

假设您$_category是以下的有效实例Mage_Catalog_Model_Category

<?php $_siblings = $_category->getParentCategory()->getChildrenCategories(); ?>
<ul>
    <?php foreach($_siblings as $_sibling): ?>
    <li>
        <?php echo $_sibling->getName(); ?>
    </li>
    <?php endforeach; ?>
</ul>
于 2012-10-23T19:36:57.787 回答