0

I an newbie to Magento. I want to load only one category in my category navigation. That root category ID is (2). I want load all the sub-categories under that root categories. and under sub-category all the product that category have. And code is working fine but its loading all the menu and categories i have in my magento website.

My Code

<?php $_menu = $this->renderCategoriesMenuHtml($this->getSelectedCategoryId(), 0, 'level-top') ?>
<?php
if ($this->getTitle() != '') {
    $title = $this->getTitle();
} else {
    $title = 'Categories';
}
?>
<?php
$products = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect('*');
?>
<?php if ($_menu): ?>
        <div class="block vertical-menu">   
            <div class="title-block" style="margin-bottom:0px;"><h4><?php echo $this->__($title) ?></h4></div>  
            <div class="block-content">
                <?php  $children = Mage::getModel('catalog/category')->getCollection(2)
                     ->addAttributeToSelect('name')
                     ->addAttributeToSelect('is_active')
                     ->addUrlRewriteToResult();?>
                <ul class="content-navigation">
                    <?php $childGroup = 0;  ?>
                    <?php foreach($children as $category){ ?>

                        <li id="menu-item-<?php echo $category->getId(); ?>">
                            <?php 
                                  $childGroup= $childGroup + 1;
                            ?>
                            <a href="<?php echo $category->getUrl($category);  /* $childGroup; echo $category->getUrl($category) */ ?>"><?php echo $category->getName();?></a>
                            <span class="toggle-icon"><a class="toggle-icon-me" href="#child-group-<?php echo $childGroup ?>" role="button" data-toggle="collapse" 
                            area-expanded="false" area-controls="#child-group-<?php echo $childGroup ?>"></a></span>

                            <?php $categoryId = $category->getId(); ?>
                            <?php $category = Mage::getModel('catalog/category')->load($categoryId);
                                    $products = Mage::getResourceModel('catalog/product_collection')
                                              ->addCategoryFilter($category)
                                              ->addAttributeToSelect('*');
                            ?>

                            <ul id="child-group-<?php echo $childGroup;  ?>" class="collapse">
                                <?php foreach($products as $product){ ?>
                                <li id="product-<?php echo $product->getId(); ?>"><a href="<?php echo $product->getUrlPath(); ?>" style="padding-left:25px;"><span style="font-size: 11px;"><?php echo $product->getName(); ?></span></a></li>
                                <?php }?>
                            </ul>
                        </li>
                    <?php } ?>
                </ul>
            </div>
        </div>
<?php else: ?>
    <?php if (Mage::helper('mpanel')->acceptToUsePanel()): ?>
        <div class="block vertical-menu">   
            <div class="block-title"><strong><span><?php echo $this->__($title) ?></span></strong></div>    
            <div class="block-content">
                <p><?php echo $this->__('Have no categories.'); ?></p>
            </div>
        </div>
    <?php endif; ?>
<?php endif; ?>

What is wrong with my code?

1
  • can you attach screen shot? Commented Nov 17, 2016 at 12:58

1 Answer 1

0

There is an attribute in the category configuration which allow you not to display them in the navigation menu.

Edit the "root categories" you don't want to show, and set the nclude in Include in Navigation Menu to No on the "General Informations" tab.

Also, there are errors in your code.

$children = Mage::getModel('catalog/category')->getCollection(2)

You cannot add 2 as a parameter.

What I think you tried is to get only the category with id=2. But as I said, you won't need to filter the collection anymore by using the native Magento attribute.

For more information about filtering a category, you should take a look at addAttributeToFilter method.

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.