问题
I have added this code {{block type="catalog/product_list" category_id="25" template="catalog/product/list.phtml"}}
in cms home page
I want to limit no of products to display to nine to this category only.How can i do that?
回答1:
I don't think there is a value you can pass into the block tag to limit it. I would suggest making a new list.phtml file that limits it there.
Let me look at the code real quick.
Ok. If you were to copy the file /app/design/frontend/default/default/template/catalog/product/list.phtml
to
/app/design/frontend/default/default/template/catalog/product/list-limit.phtml
and then edit it as follows:
LINE49: After the foreach
<?php if($_iterator >=9) { break; } ?>
LINE94: Where $_collectionSize is assigned change to:
<?php $_collectionSize = main(9, $_productCollection->count()) ?>
Line97: After the foreach
<?php if($i >= 9) { break; } ?>
It should achieve what you desire regardless of Grid or List view.
... shortly an alternative method ...
The other way would be to edit the List.php file that loads the product list that the phtml file presents. Block Type of 'catalog/product_list' means you need the file:
/app/code/core/Mage/Catalog/Block/Product/List.php
In there you will see the method getLoadedProductCollection, which calls _getProductCollection. That code could be edited to filter/limit the number of returned products. You would want to make a copy of that file though, and update the block link in your page. Don't add underscores to the name, as that will require the file be put in a subdirectory.
Hope this helped.
回答2:
Following on from the previous answer, I seem to have acheived this by editing the List.php by adding the following after line 96.
return $this->_productCollection
->setPageSize($this->getProductsCount());
}
/**
* Set how much product should be displayed at once.
*
* @param $count
* @return Mage_Catalog_Block_Product_New
*/
public function setProductsCount($count)
{
$this->_productsCount = $count;
return $this;
}
/**
* Get how much products should be displayed at once.
*
* @return int
*/
public function getProductsCount()
{
if (null === $this->_productsCount) {
$this->_productsCount = self::DEFAULT_PRODUCTS_COUNT;
}
return $this->_productsCount;
}
and adding this after line 43
/**
* Default value for products count that will be shown
*/
const DEFAULT_PRODUCTS_COUNT = 100;
/**
* Products count
*
* @var null
*/
protected $_productsCount;
I got the codes from new.php
来源:https://stackoverflow.com/questions/6760352/magento-limit-number-of-products-in-home-page