如何在magento中获得一个类别的url

vmdwslir  于 2022-11-12  发布在  其他
关注(0)|答案(1)|浏览(207)

我试图显示所有的二级类别,我的代码是

<?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类)请帮助我,我必须显示第三级菜单也当用户悬停在类别...

au9on6nz

au9on6nz1#

希望下面的代码能解决你的问题。

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

对我有用。

相关问题