我需要在magento 1.7中获得一个兄弟类别列表

时间:2012-10-23 18:58:48

标签: magento categories magento-1.7 siblings

到目前为止,我认为我正在做类似的工作

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

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

如果有意义,请告诉我。

2 个答案:

答案 0 :(得分:4)

以下代码将帮助您加载第二级(子)类别。您也可以通过 Mage :: app() - &gt; getStore() - &gt; 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...';

答案 1 :(得分:1)

假设您的$_categoryMage_Catalog_Model_Category的有效实例:

<?php $_siblings = $_category->getParentCategory()->getChildrenCategories(); ?>
<ul>
    <?php foreach($_siblings as $_sibling): ?>
    <li>
        <?php echo $_sibling->getName(); ?>
    </li>
    <?php endforeach; ?>
</ul>