24 April

How to limit number of products shown on homepage

: 3 Comments
magento

Have you ever wanted to set a specified number of products to display in your theme Homepage?, simple:

Let’s say you have a block call to a  featured-products.phtml  in your CMS Homepage content:

{{block type=”catalog/product_list” category_id=”5″ template=”catalog/product/featured-products.phtml”}}

The file  featured-products.phtml  displays all products under category with ID=5  ,but what about if you want to display only 10 products of that category?, check this:

Add this property to the line above:  is_homepage=”1″  

The line will look like:

{{block type=”catalog/product_list” is_homepage=”1″ category_id=”5″ template=”catalog/product/featured-products.phtml”}}

Now open up   featured-products.phtml  then find this line:

<?php $i=0; foreach ($_productCollection as $_product): ?>

Just below it, add this:

<?php if($this->getIsHomepage() && $i==10) break; ?>

$i==10 means it will display 10 products, you can change this value to your choice.

That’s it!, another useful Magento tip!

Cheers!! ;)

 

  • Christian Wolpers

    Hey, whats about the list.phtml ? I cant limit products there. Please help me :(

    • Abin

      do some logic trick in foreach loop. like count a variable till 10 then break the loop. That’s it :)

  • http://www.magentodedicatedserver.com/ Justin Jeffries

    this is also one of the best ways to speed up the load times of your site, see here for another 99 ways to do this http://www.magentodedicatedserver.com/speed-up-magento-page-load-times/

Back to Top