简体   繁体   中英

Magento - list sub-categories of a specific parent category as links

I am a beginner to php and am stuck on trying to call out sub-categories of just one parent category as links

I got to this and it's bringing up the getName but the getUrl() isn't returning any URL at all....

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

The output code is just <li><a href="">name of sub-cat</a></li>

Anybody have any ideas? Please?

Thanks, Kayla

Please try this code I think you did this code but this is very helpful for someone who is searching this code

<?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;
?>

I don't know why @Dhanapal solution has not worked for me, so I used:

$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;

For some reason, the above answer did not work for me. I am also using Magento 1.7.x.

I found Magento displays subcategories description on category list.phtml link to be helpful.

I am trying to do the same as you so I adjusted the above answer from

<?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>

to:

<?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>

Just like the above answer, you can change the div class to

<div class="products-grid">

instead of listing them. But you have asked how to list.

I hope this helps people in the future. There are tons of other related questions on Stack overflow as well.

Well, This was a pain in the ass. I've made a function for it that lists all the subcategories and sub-sub-categories of the category that you want to display.

    <?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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM