在magento的左侧栏上显示类别

时间:2014-02-05 06:56:56

标签: magento

我只想在左侧栏中显示类别和子类别,我有一个根据我的需要工作的代码,但是这段代码只显示了它没有得到特定类别名称的URL类别的名称。 代码: -

<?php 
$_categories = Mage::getBlockSingleton('catalog/navigation'); 
foreach ($_categories->getStoreCategories() as $_category) { 
$category = Mage::getModel('catalog/category'); 
$category->load($_category->getId()); 
$subcategories = explode(',', $category->getChildren()); 
?> 

    <?php 
    foreach ($subcategories as $subcategoryId) { 
    $category->load($subcategoryId); 
    echo '<li><a href="' . $category->getURL() . '">' . $category->getName() . '</a></li>'; 
    } 
    ?> 

<?php
}  
?>

它仅显示每个子类别的名称,但无法正确地将其与每个子类别的特定URL相关联。建议我在哪里弄错了。

1 个答案:

答案 0 :(得分:1)

您需要再次加载子类别,而不是使用类别对象:

<?php 
foreach ($subcategories as $subcategoryId) { 
//$category->load($subcategoryId); instead
$subcategory = Mage::getModel("catalog/category")->load($subcategoryId);
echo '<li><a href="' . $subcategory->getURL() . '">' . $subcategory->getName() . '</a></li>'; 
} 
?>

第二个好方法如下:

<?php $_helper = Mage::helper('catalog/category') ?>
<?php $_categories = $_helper->getStoreCategories() ?>
<?php $currentCategory = Mage::registry('current_category') ?>
<?php if (count($_categories) > 0): ?>
<ul>
    <?php foreach($_categories as $_category): ?>
        <li>
            <a href="<?php echo $_helper->getCategoryUrl($_category) ?>">
                <?php echo $_category->getName() ?>
            </a>
            <?php $_category = Mage::getModel('catalog/category')->load($_category->getId()) ?>
            <?php $_subcategories = $_category->getChildrenCategories() ?>
            <?php if (count($_subcategories) > 0): ?>
                <ul>
                    <?php foreach($_subcategories as $_subcategory): ?>
                        <li>
                            <a href="<?php echo $_helper->getCategoryUrl($_subcategory) ?>">
                                <?php echo $_subcategory->getName() ?>
                            </a>
                        </li>
                    <?php endforeach; ?>
                </ul>
            <?php endif; ?>
        </li>
    <?php endforeach; ?>
</ul>
<?php endif; ?>

请注意,您可能需要调整上面的一些代码。

希望这有帮助。