Magento - 将特定父类别的子类别列为链接

时间:2011-05-04 20:40:54

标签: php parsing magento e-commerce categories

我是php的初学者,我一直试图将一个父类别的子类别称为链接

我得到了这个,它提出了getName,但getUrl()根本没有返回任何URL ....

<?php
      $children = Mage::getModel('catalog/category')->getCategories(3);
      foreach ($children as $category):
            echo '<li><a href="' . $category->getUrl() . '">' . $category->getName() . '</a></li>';
      endforeach;
?>

输出代码只是<li><a href="">name of sub-cat</a></li>

有人有什么想法吗?请?

谢谢, 凯拉

4 个答案:

答案 0 :(得分:6)

请尝试使用此代码我认为您执行了此代码,但这对于正在搜索此代码的人非常有用

<?php
    $children = Mage::getModel('catalog/category')->getCategories(3);
    foreach ($children as $category):
        $category = Mage::getModel('catalog/category')->load($category->getId());
        echo '<li><a href="' . $category->getUrl() . '">' . $category->getName() . '</a></li>';
    endforeach;
?>

答案 1 :(得分:1)

我不知道为什么@Dhanapal解决方案对我没用,所以我用过:

$categories = Mage::getModel('catalog/category')->load('3')->getChildrenCategories();
foreach ($children as $category):
    $category = Mage::getModel('catalog/category')->load($category->getId());
    echo '<li><a href="' . $category->getUrl() . '">' . $category->getName() . '</a></li>';
endforeach;

答案 2 :(得分:0)

出于某种原因,上述答案对我不起作用。我也在使用Magento 1.7.x.

我发现Magento displays subcategories description on category list.phtml链接很有帮助。

我正在尝试和你一样,所以我从

调整了上面的答案
<?php $children = explode( ",", $this->getCurrentCategory()->getChildren() ); ?>
<div class="category-products">
    <ul class="products-grid">
        <?php foreach( $children as $child ): ?>
            <?php $_child = Mage::getModel( 'catalog/category' )->load( $child ); ?>
            <li class="item"><?php echo $_child->getDescription(); ?></li>
        <?php endforeach; ?>
    </ul>
</div>

为:

<?php $children = explode( ",", $this->getCurrentCategory()->getChildren() ); ?>
<div class="category-products">
    <ul class="products-list">
        <?php foreach( $children as $child ): ?>
            <?php $_child = Mage::getModel( 'catalog/category' )->load( $child ); ?>
            <li class="item"><a href="<?php echo $_child->getUrl() ?>"> <?php echo     $_child->getName() ?> </a></li>
        <?php endforeach; ?>
    </ul>
</div>

就像上面的回答一样,您可以将div类更改为

<div class="products-grid">

而不是列出它们。但你问过如何列出。

我希望这有助于将来的人们。 Stack溢出还有很多其他相关问题。

答案 3 :(得分:0)

嗯,这是屁股的痛苦。我已经为它创建了一个函数,列出了要显示的类别的所有子类别和子子类别。

    <?PHP

//get the children of the current category
function getChildrenInterStore($id) {
    $returnstring = '';
    $subCats = Mage::getModel('catalog/category')->load($id)->getChildren();
    //get sub category ids
    $subCatIds = explode(',',$subCats);
    if (count($subCatIds) > 0): 
        foreach($subCatIds as $subCatId): 
            $subCat = Mage::getModel('catalog/category')->load($subCatId); 
            if($subCat->getIsActive()): 
                $returnstring .= '
                <li class="other-toggle sm_megamenu_lv1 sm_megamenu_drop parent">
                    <a class="sm_megamenu_head sm_megamenu_drop" href="'.$subCat->getUrl().'">
                        <span class="sm_megamenu_icon">
                        <span class="sm_megamenu_title">'.$subCat->getName().'</span>
                    </span>
                    </a>
                </li>';
                $returnstring .= getChildrenInterStore($subCatId);  
            endif; 
        endforeach;
    endif;
    return $returnstring; 
}
?>

<div class="mega-left-title">
    <strong>Categorieen</strong>
</div>

<div class="css_effect sm_megamenu_wrapper_vertical_menu sambar">
    <div class="sambar-inner">
        <ul class="sm-megamenu-hover sm_megamenu_menu sm_megamenu_menu_black">
            <?PHP echo getChildrenInterStore('17'); ?>
        </ul>
    </div>  
</div>