1

Working on the categories for a magento homepage for pulling out categories on the main page.

At the moment it pulls all out of the categories out but I want to limit this too 6 categories.

I have tried a few different options and have not been able to get this working correctly.

What would be the best way to apply the limit? I have included code below.

<ul class="cat-nav">
<?php
    $obj = new Mage_Catalog_Block_Navigation();
    $storeCategories = $obj->getStoreCategories();
    Mage::registry('current_category') ? $currentCategoryId = Mage::registry('current_category')->getId() : $currentCategoryId='';
    foreach ($storeCategories as $_category):
?>
        <li class="<?php echo $_category->getName(); ?>">

            <?php $categoryChildren = $_category->getChildren(); ?>
            <?php if($categoryChildren->count()) : ?>
                <ul>

                    <?php foreach($categoryChildren as $_categoryChild) : ?>
                        <?php $_categoryChildModel = Mage::getModel('catalog/category')->load($_categoryChild->getId());?>
                        <?php $categoryGrandchildren=$_categoryChild->getChildren(); ?>
                        <li class="<?php echo $_category->getName(); ?>">
                            <?php
                                $currentCategoryId===$_categoryChild->getId() ? $bold="style=\"font-weight:bold\"" : $bold='';
                                echo '&emsp;' . '<a href="' . $_categoryChildModel->getUrl() . '"' . $bold . '>' .  $_categoryChild->getName() . '' . $_categoryChildModel->getProductCollection() . '</a>';
                            ?>
                        </li>
                        <?php if($categoryGrandchildren->count()) : ?>
                            <?php foreach($categoryGrandchildren as $_categoryGrandchild) : ?>
                                <?php $_categoryGrandchildModel = Mage::getModel('catalog/category')->load($_categoryGrandchild->getId());?>
                                <li>
                                    <?php
                                        $currentCategoryId===$_categoryChild->getId() ? $bold="style=\"font-weight:bold\"" : $bold='';
                                        echo '&emsp;&emsp;' . '<a href="' . $_categoryGrandchildModel->getUrl() . '"' . $bold . '>' .  $_categoryGrandchild->getName() . '(' . $_categoryGrandchildModel->getProductCount() . ')</a>';
                                    ?>
                                </li>
                            <?php endforeach; ?>
                        <?php endif; ?>
                    <?php endforeach; ?>
                </ul>
            <?php endif; ?>
        </li>
    <?php endforeach ?>
</ul>
  • Hi, welcome on Magento SE. Use a category collection, it'll give you more control over the limit rather than resorting to use a counter and if's :) – Julien Lachal Jul 23 '15 at 11:57
  • Hi Im not too sure as to how to do this? I thought this would be the better way and just limiting them within the foreach loop – Robert Jul 23 '15 at 12:04
  • easier yet dirtier. here is a link for the manipulation of products to give you a rough idea of what you should do. – Julien Lachal Jul 23 '15 at 12:12
5

You can get a list of categories based on the parent id like this:

$parentId = 'Your parent id here';
$categories = Mage::getModel('catalog/category')
    ->getCollection()
    ->addAttributeToSelect('*')
    ->addAttributeToFilter('parent_id', $parentId)
    ->addAttributeToFilter('is_active', 1)
    ->addAttributeToSort('position')
    ->setCurPage(1)->setPageSize(6);//limit to 6 categories

To get the root categories use $parentId = Mage::app()->getStore()->getRootCategoryId().
For the sub categories use $parentId = 'the category id for which you are trying to retrieve the child categories.'

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.