0

I'm listing out my categories on the homepage, but I only want to list out 7 categories on the homepage for now. Here is my code:

<?php
$categories = $this->getStoreCategories(true,false,true);
$categoryHelper = $this->getCategoryHelper();
?>
<ul class="category-list">
<?php
foreach($categories as $category):
     if (!$category->getIsActive()) {
        continue;
     }

?>
<li class="category-item"><a href="<?php echo $categoryHelper->getCategoryUrl($category) ?>"><?php echo $category->getName() ?></a>
<?php if($childrenCategories = $this->getChildCategories($category)): ?>
    <ul>
        <?php
        foreach($childrenCategories as $childrenCategory):
             if (!$childrenCategory->getIsActive()) {
                continue;
             }
        ?>
            <li><a href="<?php echo $categoryHelper->getCategoryUrl($childrenCategory) ?>"><?php echo $childrenCategory->getName() ?></a></li>
        <?php endforeach; ?>
    </ul>
<?php
endif;
endforeach;
?>
</li>
</ul>

I'd also like to limit the child categories to only 3. I know this is probably a simple php edit, but I'm currently stuck on it.

1

You just have to insert an iterator in both loops and break it at 7 and 3:

<?php
$categories = $this->getStoreCategories(true,false,true);
$categoryHelper = $this->getCategoryHelper();
?>
<ul class="category-list">
<?php
$i = 0;
foreach($categories as $category):
     if (!$category->getIsActive()) {
        continue;
     }
     if($i == 7){
        break; //break it at seven
     }
     $i++;

?>
<li class="category-item"><a href="<?php echo $categoryHelper->getCategoryUrl($category) ?>"><?php echo $category->getName() ?></a>
<?php if($childrenCategories = $this->getChildCategories($category)): ?>
    <ul>
        <?php
        $child_number = 0;
        foreach($childrenCategories as $childrenCategory):
             if (!$childrenCategory->getIsActive()) {
                continue;
             }
             if($child_number == 3){
                break; //break it at 3 for children
             }
             $child_number++;
        ?>
            <li><a href="<?php echo $categoryHelper->getCategoryUrl($childrenCategory) ?>"><?php echo $childrenCategory->getName() ?></a></li>
        <?php endforeach; ?>
    </ul>
<?php
endif;
endforeach;
?>
</li>
</ul>
3
  • Is there any function to limit only 4 sub categories collection? – jafar pinjar Nov 10 '18 at 7:45
  • @jafarpinjar yes – Claudiu Creanga Nov 10 '18 at 20:07
  • Yes, pls help me with code, how to limit to 6 only? – jafar pinjar Nov 11 '18 at 4:14
0

There has to be a more efficient way to achieve this?

What's the point in dragging all the category data from the database if you just want to display a couple of categories.

I'm trying to figure out the same thing, I have a store that could potentially have 100's of categories but I want to display 8 categories on the homepage.

So far I've found you can use the same method as you've suggested, but as you said, that doesn't seem to allow us to add a limit.

I've also tried using the categoryCollectionFactory, which does allow me to set a limit, but drags in the "Default Category", the top level category which I don't want. So now I need to figure out a way to ignore that.

I don't want to use PHP in my view to break out of a loop as suggested by Claudiu because it's super inefficient.

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.