4

I am trying to limit the number of products from a certain category to 4 on my homepage.

The code I am trying to do this with is:

{{block type="catalog/product_list" column_count="4" category_id="13" template="catalog/product/list.phtml"}}

Here are some of the things I have tried:

num_products="4"
limit = 4, limit="4"
count = 4, count="4"
_productCollection="4"
_productsCount="4"

I have made a copy of list.phtml thinking there might be a way to change it in there, but was unable to find out a way.

At the very top pf list.phtml is this code:

<?php
    $_productCollection=$this->getLoadedProductCollection();
    $_helper = $this->helper('catalog/output');
?>

And under grid view there is this:

<?php $_collectionSize = $_productCollection->count() ?>
    <?php $_columnCount = $this->getColumnCount(); ?>
    <?php $i=0; foreach ($_productCollection as $_product): ?>
        <?php if ($i++%$_columnCount==0): ?>

Any ideas on to limit the products either within the block or the template file?

4

5 回答 5

5

更快的被替换column_count=4is_homepage=1

并在 phtml 中添加:

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

在这之前 :

 <?php if ($i++%$_columnCount==0): ?>

然后你的主页上只有 1 行(如果我想是 4 行)所以总共 4 个产品

于 2013-03-07T20:33:53.470 回答
3

尝试

 {{block type="catalog/product_list" limit="4" category_id="13" template="catalog/product/list.phtml"}}

于 2013-03-07T20:34:03.153 回答
2

在 Magento 版本中。1.9.0.1 我找到了一个简单的解决方案,只需在 list.phtml 中添加这些行,只需找到它出现两次的 foreach 循环,因此需要在两个位置添加。

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

我把它放了 6 条记录,您可以根据需要更改它。

谢谢

于 2014-07-16T13:10:21.317 回答
1

尝试这个: $_productCollection=$this->getLoadedProductCollection(); $_productCollection->getSelect()->limit(5);

于 2014-03-04T05:08:11.990 回答
1

当我遇到这个问题时,他们搜索了很多网站,但让我理解它的网站却很少。我自己编辑了它,通过执行这些步骤来显示来自某些类别的产品的固定数量如下:- 转到

1) app\design\frontend\default\<your theme>\template\catalog\product
   copy list.phtml and save as list_new.phtml

现在打开 list_new.phtml 并在结束 if 循环后搜索 '' 插入此代码

<?php if($i<=4): // for 4 product?>

并在列表结束后关闭。

<?php endif // for 4 product?>

代码将如下所示 -

<?php if ($i++%$_columnCount==0): ?>
    <ul class="products-grid">
<?php endif ?>
<?php if($i<=4): // for 4 product?>
    <li class="item<?php if(($i-1)%$_columnCount==0): ?>"> 
        // some code here
     </li>
<?php endif // for 4 product?>

2) 现在转到 CMS>Pages>select home page>content> 并复制此代码(在此处更改您的类别 ID)

{{block type="catalog/product_list" name="product_list" category_id="<category id>" column_count="4"mode="grid" template="catalog/product/list_new.phtml"}}
于 2013-11-06T05:38:09.463 回答