Show Specific Categories On Magento Home Page


Hope someone can help me with this. I need to show 3 specific categories on my Magento home page. I have created a “list-homepage.phtml” file and have the following code in this file

<?php foreach ($this->getStoreCategories() as $_category): ?>
<?php $open = $this->isCategoryActive($_category); ?>
$layer = Mage::getSingleton('catalog/layer');
if ($immagine = $this->getCurrentCategory()->getImageUrl()):
<div class="catalog-image">
    <div class="col-sm-4">
        <a href="<?php echo $this->getCategoryUrl($_category)?>">
        <img src="<?php echo $immagine ?>" alt="<?php echo $this->htmlEscape($this->getCurrentCategory()->getName()) ?>" class="img-responsive" />
    <!--<div class="left"><h2><a href="<?php echo $this->getCategoryUrl($_category)?>"><?php echo $_category->getName()?></a></h2></div> -->
<?php endif; ?>
<?php endforeach; ?>

And I have added this block to my home page in the back-end

{{block type="catalog/navigation" name="catalog.category" template="catalog/category/list-homepage.phtml"}}

The problem with this is that it displays all the categories I have, I just want to display 3, what do I have to do in order to achieve this?

Many Thanks for your help

You’ll need to stop the loop from completing the iteration if the current category is not wanted. One way you could do this is to note the ID of the 3 categories and use them to skip the loop iteration:

  $arr_cat_ids = array('2','4','6'); //array of wanted category ids
  foreach ($this->getStoreCategories() as $_category):
    $this_cat_id = $_category->getId();
    if(!in_array( $this_cat_id, $arr_cat_ids)) continue; //if category is unwanted, skip to next iteration

Thanks for the reply. Where would I add this into my code example above?

Replace the first line of code with the new code in my post.


That’s great and it now works!!

Thanks again, really appreciate it.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.