Magento for each loop limit The Next CEO of Stack OverflowLimit for creating storesTop Menu cache problemCreate categories through installerScript to hide empty categories works but not with products without stockHow to change the memory limit for phpPersonal discount for each customerMagento memory Limit for admin reportMagento 2: Set Category Limit in ListLoop Through Magento Categories, Meta Keywords and DescriptionsFor each loop running only once

Complex fractions

How do I go from 300 unfinished/half written blog posts, to published posts?

"In the right combination" vs "with the right combination"?

Is it my responsibility to learn a new technology in my own time my employer wants to implement?

Contours of a clandestine nature

MessageLevel in QGIS3

Which tube will fit a -(700 x 25c) wheel?

Are there any limitations on attacking while grappling?

Why does the UK parliament need a vote on the political declaration?

How do I make a variable always equal to the result of some calculations?

What is "(CFMCC)" on an ILS approach chart?

Should I tutor a student who I know has cheated on their homework?

How do scammers retract money, while you can’t?

What benefits would be gained by using human laborers instead of drones in deep sea mining?

Elegant way to replace substring in a regex with optional groups in Python?

How to prevent changing the value of variable?

How to safely derail a train during transit?

Real integral using residue theorem - why doesn't this work?

WOW air has ceased operation, can I get my tickets refunded?

Would this house-rule that treats advantage as a +1 to the roll instead (and disadvantage as -1) and allows them to stack be balanced?

Is it ever safe to open a suspicious html file (e.g. email attachment)?

Written every which way

Novel about a guy who is possessed by the divine essence and the world ends?

What's the best way to handle refactoring a big file?



Magento for each loop limit



The Next CEO of Stack OverflowLimit for creating storesTop Menu cache problemCreate categories through installerScript to hide empty categories works but not with products without stockHow to change the memory limit for phpPersonal discount for each customerMagento memory Limit for admin reportMagento 2: Set Category Limit in ListLoop Through Magento Categories, Meta Keywords and DescriptionsFor each loop running only once










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 10 mins 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

            Disable / Remove link to Product Items in Cart Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Announcing the arrival of Valued Associate #679: Cesar Manara Unicorn Meta Zoo #1: Why another podcast?How can I limit products that can be bought / added to cart?Remove item from cartHide “Add to Cart” button if specific products are already in cart“Prettifying” the custom options in cart pageCreate link in cart sidebar to view all added items After limit reachedLink products together in checkout/cartHow to Get product from cart and add it againHide action-edit on cart page if simple productRemoving Cart items - ObserverRemove wishlist items when added to cart

            Helsingin valtaus Sisällysluettelo Taustaa | Yleistä sotatoimista | Osapuolet | Taistelut Helsingin ympäristössä | Punaisten antautumissuunnitelma | Taistelujen kulku Helsingissä | Valtauksen jälkeen | Tappiot | Muistaminen | Kirjallisuutta | Lähteet | Aiheesta muualla | NavigointivalikkoTeoksen verkkoversioTeoksen verkkoversioGoogle BooksSisällissota Helsingissä päättyi tasan 95 vuotta sittenSaksalaisten ylivoima jyräsi punaisen HelsinginSuomalaiset kuvaavat sotien jälkiä kaupungeissa – katso kuvat ja tarinat tutuilta kulmiltaHelsingin valtaus 90 vuotta sittenSaksalaiset valtasivat HelsinginHyökkäys HelsinkiinHelsingin valtaus 12.–13.4. 1918Saksalaiset käyttivät ihmiskilpiä Helsingin valtauksessa 1918Teoksen verkkoversioTeoksen verkkoversioSaksalaiset hyökkäävät Etelä-SuomeenTaistelut LeppävaarassaSotilaat ja taistelutLeppävaara 1918 huhtikuussa. KapinatarinaHelsingin taistelut 1918Saksalaisten voitonparaati HelsingissäHelsingin valtausta juhlittiinSaksalaisten Helsinki vuonna 1918Helsingin taistelussa kaatuneet valkokaartilaisetHelsinkiin haudatut taisteluissa kaatuneet punaiset12.4.1918 Helsingin valtauksessa saksalaiset apujoukot vapauttavat kaupunginVapaussodan muistomerkkejä Helsingissä ja pääkaupunkiseudullaCrescendo / Vuoden 1918 Kansalaissodan uhrien muistomerkkim

            Adjektiivitarina Tarinan tekeminen | Esimerkki: ennen | Esimerkki: jälkeen | Navigointivalikko