I am trying to get only parent categories of a category id 4 but right now instead of parent category it is listing all child categories. Below is the code that I currently have in .phtml template file. Is there something wrong in the code? How could I achieve it?

<?php $_helper = Mage::helper('catalog/category') ?>
<?php $_categories = Mage::getModel('catalog/category')->getCategories(4); ?>
<?php $currentCategory = Mage::registry('current_category') ?>
<?php if (count($_categories) > 0): ?>
    <?php foreach($_categories as $_category): ?>
            //arrange by Letter
            foreach($_category->getChildren() as $_sub){
                $letter = substr(strtolower($_sub->getName()),0,1);
                $subCategories[$letter][0]['name'] = $letter; //0 index is the letter
                $i = 1;
                $subCategories[$letter][$i]['name'] = $_sub->getName();
                $subCategories[$letter][$i]['url'] = $_helper->getCategoryUrl($_sub);

    <?php endforeach ?>
        $i = 0;
        $x = 0;
        $d = 0;
        $splits = 1;
        foreach($subCategories as $sub){
            $d += count($sub); //split it into 3rds
        $d = round($d / 5);
        //get a count of all elements for splitting
        foreach($subCategories as $sub){
            $i = 0;
            //$d = round(count($sub) / 3); //split it into 3rds

            echo '<div class="catContainer">';
            $displayHead = TRUE;
                if($x % $d == 0 && $x != 0 && $splits != 5){
                    echo '</div></div><div class="containerContainer"><div class="catContainer">';
                    if($i != 0){
                        echo '<div class="catContainerTop">'.$sub[0]['name'].'</div>';
                if($i == 0){
                    echo '<div class="catContainerTop">'.$sub[0]['name'].'</div>';
                } else if($i != 0){
                    echo '<span><a href="'.$sub[$i]['url'].'">' . $sub[$i]['name'] . '</a></span><br />';
            echo '</div>';
<?php endif; ?>

Update: I did try adding <?php $subCategories = array(); ?> before <?php if (count($_categories) > 0): ?> as suggested but it is not helping at all so anyone with the suggestion and solutions?

  • 1
    add $subCategories =array(); before if (count($_categories) > 0): ?> – Amit Bera Oct 11 '14 at 7:50
  • @AmitBera Thanks, I did added <?php $subCategories = array(); ?> before <?php if (count($_categories) > 0): ?> and it didn't work. – Kevin S Oct 11 '14 at 10:32

Your code actually does not make sense to me. You say you want to load category with id 4, but the code does not reflect that.

To load category with ID 4, you'd use $currentCategory = mage::getModel('catalog/category')->load(4)

which would then give you one category object.

Then to get its parent categories, you use the getPath() call on that category object. This will result in a string in format 'id/id/id/id' which is the path of categories of category with id 4, up to your base category.

Thus, to get the parent categories of category id 4, you'd explode that to an array, which then will give you an array of parent categories (up the tree) to work with.

Ideally you'd not want to do a loop and load each of them, but do one collection to load them all as a collection.

Just as a hint. You are placing too much logic into your phtml. You should create a block class and move the business logic into the block class. In the long run that will be much more maintainable.

Good luck

  • Thanks, I am sorry about that. I am trying to get sub categories of category id 4 but right now instead of subcategories it is showing children categories of sub categories. – Kevin S Oct 12 '14 at 9:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.