如何在magento中获取类别的URL

时间:2013-08-16 07:07:32

标签: magento magento-1.7

我正在尝试显示所有二级类别,而我的代码是

<?php $storeId    = Mage::app()->getStore()->getId();

//Get category model
$_category = Mage::getModel('catalog/category')->setStoreId($storeId);

$_categoryCollection = $_category->getCollection();
$_categoryCollectionIds = $_categoryCollection->getAllIds();

//Remove root category from array
unset($_categoryCollectionIds[0], $_categoryCollectionIds[1]);

?>

<div id="accordian_hover">
<?php

$o = null;
$o .= '<ul>';

foreach ($_categoryCollectionIds as $catId) {

    $_category = $_category->load($catId);

        if($_category->getLevel() == 2) {

            $catChildren = $_category->getChildren();          

                if(!empty($catChildren)) {
                    $o .= '<li> <a href="'.$_category->getUrl().'">'.$_category->getName().'</a>';
                    $o .= '<ul>';

                    $categoryChildren = explode(",", $catChildren);

                    foreach ($categoryChildren as $categoryChildId) {

                        /* @var $_childCategory Mage_Catalog_Model_Category */
                        $_childCategory = $_category = Mage::getModel('catalog/category')->setStoreId($storeId)->load($categoryChildId);

                        $o .= '<li><a href="'.$_childCategory->getUrl().'">'.$_childCategory->getName().'</a></li>';

                        // If you wish to display the total number of products for each category, uncomment this line
                        // $o .= '<span class="totalNumberOfProducts">'.$_childCategory->getProductCollection()->count().'</span>';

                    }

                    $o .= '</ul></li>';
                }   

        }
    } $o .='</ul>';


echo $o;
?>
</div>

但是顶级菜单网址错误所有其他显示正确但主类别网址错误(第2类)请帮助我,当用户将鼠标悬停在类别上时我也必须显示第三级菜单... 实时链接http://toolskaart.com/

1 个答案:

答案 0 :(得分:0)

希望以下代码能解决您的问题。

Mage::getModel('catalog/category')->load($_category->getId())->getUrl()

它对我有用。