Magento for each loop limit












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>









share|improve this question

























  • 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
















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>









share|improve this question

























  • 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














1












1








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>









share|improve this question
















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>






magento-1.9 category php






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 1 min ago









Teja Bhagavan Kollepara

3,01241949




3,01241949










asked Jul 23 '15 at 11:53









RobertRobert

61




61













  • 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



















  • 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

















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





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





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










1 Answer
1






active

oldest

votes


















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.'






share|improve this answer
























    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "479"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f75446%2fmagento-for-each-loop-limit%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    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.'






    share|improve this answer




























      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.'






      share|improve this answer


























        5












        5








        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.'






        share|improve this answer













        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.'







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jul 23 '15 at 12:08









        MariusMarius

        167k28319686




        167k28319686






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Magento Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f75446%2fmagento-for-each-loop-limit%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            What other Star Trek series did the main TNG cast show up in?

            Berlina muro

            Berlina aerponto