如何使用Magento块显示多个类别 [英] How to display multiple categories with a Magento block
问题描述
我的Magento主页上当前有此代码段,可以很好地显示所有产品.
My Magento homepage currently has this code snippet that displays all products just fine.
{{block type="catalog/product_list" category_id="2" template="catalog/product/list.phtml"}}
大致上,我的类别树看起来与此类似
Roughly, my category tree looks similar to this
id 2 (root cat)
-> id 3
-> id 4
-> id 5
由于我添加的每个产品都是ID为2的子级-每个产品都会显示在首页上.我需要的是一个解决方案,可以让我从首页产品列表中排除特定ID(类别).
Since every product I add is a child of id 2 - every product shows up on the homepage. What I'm after is a solution that will allow me to exclude specific id's (categories) from the home page product list.
我尝试了以下代码片段,但均未成功:
I've tried this snippet below with no success:
{{block type="catalog/product_list" category_id="2,3,5" template="catalog/product/list.phtml"}}
推荐答案
您的代码{{block type="catalog/product_list" category_id="2,3,5" template="catalog/product/list.phtml"}}
将不起作用,因为块Mage_Catalog_Block_Product_List仅加载一个类别$category = Mage::getModel('catalog/category')->load($this->getCategoryId());
.
Your code {{block type="catalog/product_list" category_id="2,3,5" template="catalog/product/list.phtml"}}
won't work because the block Mage_Catalog_Block_Product_List load only one category $category = Mage::getModel('catalog/category')->load($this->getCategoryId());
.
我为您的问题提供了两种解决方案,您可以使用不同的类别ID多次使用该块:
I see two solutions for your problem, you can use the block more than once with a different category id:
{{block type="catalog/product_list" category_id="2" template="catalog/product/list.phtml"}}
{{block type="catalog/product_list" category_id="3" template="catalog/product/list.phtml"}}
{{block type="catalog/product_list" category_id="5" template="catalog/product/list.phtml"}}
或覆盖块Mage_Catalog_Block_Product_List并更改此零件的行为
Or overwrite the block Mage_Catalog_Block_Product_List and change the behavior of this part
if ($this->getCategoryId()) {
$category = Mage::getModel('catalog/category')->load($this->getCategoryId());
if ($category->getId()) {
$origCategory = $layer->getCurrentCategory();
$layer->setCurrentCategory($category);
}
}
$this->_productCollection = $layer->getProductCollection();
$this->prepareSortableFieldsByCategory($layer->getCurrentCategory());
if ($origCategory) {
$layer->setCurrentCategory($origCategory);
}
这篇关于如何使用Magento块显示多个类别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!