1

I have a code and a part of it. But I do not know how to combine. I try to show the subcategory of the category ID 3.

<div class="block block-layered-nav block-layered-nav--no-filters">    
<div class="block-content toggle-content open">    
    <dl id="narrow-by-list2">    
        <dt><?php echo $this->__('Меню') ?></dt>    
        <dd>    
            <ol>    
            <?php foreach ($_categories as $_category): ?>    
                <?php if($_category->getIsActive()): ?>    
                <li>    
                    <a href="<?php echo $this->getCategoryUrl($_category) ?>"<?php if ($this->isCategoryActive($_category)): ?> class="current"<?php endif; ?>>    
                        <?php echo $this->escapeHtml($_category->getName()) ?>    
                        <span class="count">(<?php echo $_category->getProductCount() ?>)</span>    
                    </a>    
                </li>    
                <?php endif; ?>    
            <?php endforeach ?>    
            </ol>    
        </dd>    
    </dl>    
    <script type="text/javascript">decorateDataList('narrow-by-list2')</script>    
</div>    


And this line:$categoryId = 3; $category = Mage::getModel('catalog/category')->load($categoryId); $childrenCategories = $category->getChildrenCategories();

1 Answer 1

1

This is in catalog/navigation/left.phtml, correct? A few lines above the HTML that you posted you should see this line:

<?php $_categories = $this->getCurrentChildCategories() ?>

Replace that with your code, changing your $childrenCategories to $_categories, like so:

<?php
$categoryId = 3;
$category = Mage::getModel('catalog/category')->load($categoryId);
$_categories = $category->getChildrenCategories();
?>
3
  • Tell me, please, how to show all subcategories? Category ID3 (level 1) -> Category (level 2) -> Subcategories (level 3)
    – Sylon
    Dec 23, 2015 at 9:38
  • Sorry, didn't see this comment at the time. If you want to show all categories at all levels you'll need a recursive function, something like: function listAllSubs($catId) { $cat = Mage::getModel('catalog/category')->load($catId); $subs = $category->getChildrenCategories(); if ($subs->getSize()) { echo '<ul>'; foreach ($subs as $sub) { echo '<li>'.$sub->getName().'</li>'; listAllSubs($sub->getId()); } echo '</ul>'; } } $rootCatId = 1; /* or whatever it is in your store */ listAllSubs($rootCatId); Jun 28, 2017 at 8:46
  • ^^ by the way the above comment is my quick and dirty "answer" that doesn't take certain implications into account (such as how long it takes to run, how much memory it uses etc). If anyone wants me to explain in more detail and with better formatting please post a question (better yet, find an existing one, it's bound to be out there already) and add a link to that question in this comment thread. I'll try to answer a bit quicker than I did to @Agestor's query above! Jun 28, 2017 at 8:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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