限制主页上显示的产品数量,list.phtml

时间:2013-03-07 20:26:22

标签: magento block php

我试图在我的主页上将某个类别的产品数量限制为4个。

我尝试使用的代码是:

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

以下是我尝试过的一些事情:

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

我已经制作了list.phtml的副本,认为可能有办法在那里更改它,但无法找到方法。

在最顶层的pf list.phtml是这段代码:

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

在网格视图下有这样的:

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

有关在块或模板文件中限制产品的任何想法吗?

5 个答案:

答案 0 :(得分:5)

更快的是column_count=4

替换is_homepage=1

并在phtml中添加:

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

之前:

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

然后你将在主页上只有一行(如果我想的话是4行)所以总计4个产品

答案 1 :(得分:2)

尝试

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

答案 2 :(得分:2)

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

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&gt;页面&gt;选择主页&gt;内容&gt;并复制此代码(在此更改您的categy ID)

{{block type="catalog/product_list" name="product_list" category_id="<category id>" column_count="4"mode="grid" template="catalog/product/list_new.phtml"}}

答案 3 :(得分:2)

在Magento ver。 1.9.0.1 我找到了一个简单的解决方案,只需在list.phtml中添加这些行 只需要找到两次foreach循环,所以需要在两个位置添加。

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

我把它记录为6记录,你可以根据需要将其改为。

感谢

答案 4 :(得分:1)

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

相关问题