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

            Can not update quote_id field of “quote_item” table magento 2Magento 2.1 - We can't remove the item. (Shopping Cart doesnt allow us to remove items before becomes empty)Add value for custom quote item attribute using REST apiREST API endpoint v1/carts/cartId/items always returns error messageCorrect way to save entries to databaseHow to remove all associated quote objects of a customer completelyMagento 2 - Save value from custom input field to quote_itemGet quote_item data using quote id and product id filter in Magento 2How to set additional data to quote_item table from controller in Magento 2?What is the purpose of additional_data column in quote_item table in magento2Set Custom Price to Quote item magento2 from controller

            Magento 2 disable Secret Key on URL's from terminal The Next CEO of Stack OverflowMagento 2 Shortcut/GUI tool to perform commandline tasks for windowsIn menu add configuration linkMagento oAuth : Generating access token and access secretMagento 2 security key issue in Third-Party API redirect URIPublic actions in admin controllersHow to Disable Cache in Custom WidgetURL Key not changing in Magento 2Product URL Key gets deleted when importing custom options - Magento 2Problem with reindex terminalMagento 2 - bin/magento Commands not working in Cpanel Terminal

            Aasi (pallopeli) Navigointivalikko