Magento - 显示主要类别

时间:2012-07-04 14:26:20

标签: magento

我想创建一个自定义导航栏以放入我的header.phtml文件。

为此,我需要一个包含所有类别的列表,我希望有一个简单的函数可以调用来获取数组?

2 个答案:

答案 0 :(得分:3)

以下获取指定类别的子类别,您应该能够在这里工作我认为:

$layer = Mage::getSingleton('catalog/layer');
$_category = Mage::getModel('catalog/category')->load(3); //this is cat 3, or can use: 
$_category = $layer->getCurrentCategory();
$_categories = $_category->getCollection()
 ->addAttributeToSelect(array('url_key','name','image','all_children','is_anchor','description')) //or whatever fields you want
 ->addAttributeToFilter('is_active', 1)
 ->addIdFilter($_category->getChildren())
 ->setOrder('position', 'ASC')
 ->joinUrlRewrite();

然后像:

<?php foreach ($_categories as $_category): ?>

等...

您可能也有兴趣:

$this->htmlEscape($_category->getImageUrl())
$this->htmlEscape($_category->getName())
$_category->getURL()

希望有助于指出正确的方向。显然,load(3)可以只是您的根类别。

答案 1 :(得分:1)

如果您使用单一商店或将 $ _ rootCatId 更改为动态,请使用以下代码。

    $_rootCatId     = 2;
    $_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;">';Categories();
                $__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...';