如何在主页中仅显示一个类别的产品


how to display only one category of products in home page

我在查看主页上的产品时遇到问题。更准确地说,如果我使用以下代码:

$_productCollection=$this->getLoadedProductCollection();    

我收到消息"没有与所选产品匹配的产品"。

但是如果我用这个替换上面的代码:

$_productCollection = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect('*')->addAttributeToFilter('visibility', 4)->addAttributeToFilter('status', 1)->load();    

然后,即使我在主页上指定了目录,我也会显示目录中的所有产品,以这种方式显示哪个类别可以查看更多:

{{block type="catalog/product_list" category_id="24" template="catalog/product/list-home.phtml"}}    

我该如何解决它?

附言当我从导航栏中选择产品类别时,我遇到了同样的问题。

提前感谢您的帮助。

****编辑****

我在 CMS 主页中使用的代码是:

{{block type="catalog/product_list" category_id="24" template="catalog/product/list-home.phtml"}}    

使用文件list-home.phtml的代码是这样的:

<?php    
$_productCollection=$this->getLoadedProductCollection();    
$_helper = $this->helper('catalog/output');?>
<?php if(!$_productCollection->count()): ?>
<p class="note-msg"><?php echo $this->__('There are no products matching the selection.') ?></p>
<?php else: ?>
<div class="category-products">
<?php $_collectionSize = $_productCollection->count() ?>
<?php $_columnCount = $this->getColumnCount();?>
<ul class="products-grid row">
<?php $i=0; foreach ($_productCollection as $_product): ?>
<?php /* if ($i++%$_columnCount==0): ?>
<ul class="products-grid row">
<?php endif */ ?>
<li class="item<?php if(($i-1)%$_columnCount==0): ?> first
<?php elseif($i%$_columnCount==0): ?> last<?php endif; ?>  col-sm-4 col-xs-6"> 
<div class="item-container img-thumbnail">
<div class="item-flipper">
<div class="item-front">
<!-- front content -->
<a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" class="product-image"><img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(250,310); ?>" alt="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" class="img-responsive" /></a>
</div>
<div class="item-back">
<!-- back content -->
<div class="book-info">
<div class="h4"><a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($_product->getName(), null, true) ?>"><?php echo $_helper->productAttribute($_product, $_product->getName(), 'name') ?></a></div>
<div class="short-description"><?php echo $_helper->productAttribute($_product, $_product->getShortDescription(), 'short_description') ?></div>
</div>
<p><a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($_product->getName(), null, true) ?>" class="btn btn-block btn-success"><?php echo $this->__('Book Details')></a></p>
</div>
</div>
</div>
<h3 class="panel-title product-name"><a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($_product->getName(), null, true) ?>"><?php echo $_helper->productAttribute($_product, $_product->getName(), 'name') ?></a></h3>
<div class="pull-left"> <?php echo $this->getPriceHtml($_product, true) ?> </div>
<div class="pull-right">
<?php if($_product->isSaleable()): ?>
<button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="btn btn-warning btn-cart" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><?php echo $this->__('Add to Cart') ?></button>
<?php else: ?>
<p class="btn btn-alert availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
<?php endif; ?>
</div>
</li>
<?php if ($i==6) break; ?>
<?php /*  if ($i%$_columnCount==0 || $i==$_collectionSize): ?>
</ul>
<?php endif */ ?>
<?php endforeach ?></ul>
</div>
<?php endif; ?>    

用上面的代码我看不到任何产品。如果我替换开头所说的代码,那么我可以看到所有产品,但我只想显示属于我在主页代码中指定的类别的产品。

我不明白我错在哪里。

您可以使用

此代码

{{block type="catalog/product_list" name="catalog.product.list" alias="products_homepage" category_id="5" template="catalog/product/list.phtml"}}

我们可以通过转到 Admin panel > CMS > Home ,并在其中添加以下代码来轻松设置它。

{{block type="catalog/product_list" category_id="1" template="catalog/product/list.phtml"}}

其中category_id = 1是要在主页上显示的类别。您可以从Magento管理员中的类别页面获取类别ID。

希望这会有所帮助。

尝试将类别页面设置为主页..,转到Admin System->Configuration->Web->Default pages(tab)->Default web url 在该字段中,将要在主页中显示的页面的类别 ID 作为catalog/category/view/**id**