我在我的產品頁面上的list_single.phtml上有此代碼。該代碼顯示了來自類別ID 100的15個產品。但是,我想要顯示產品類別中的產品。 當我將addCategoryFilter($ _ category)更改爲 - > addCategoryFilter(4)時,它不起作用。有什麼問題?Magento按類別顯示產品
<div class="products_single newProductsContainer container">
<?php
$_helper = $this->helper('catalog/output');
$_category = Mage::getModel('catalog/category')->load(100);
$_productCollection = Mage::getResourceModel('reports/product_collection')
->addAttributeToSelect('*')
->addCategoryFilter($_category)
->setVisibility(array(2,3,4));
$_productCollection->getSelect()->order(new Zend_Db_Expr('RAND()'));
$_productCollection->setPage(1, 15);
$i=0; $u=0;
foreach ($_productCollection as $_product):
$u++;
if ($u % 15 == 1) {echo '<div class="row productsrow">';}
?>
<div class="item col-md-3 col-xs-12 col-sm-6">
<?php $dynamicproductload = " "; ?>
<a class="product-image visible-xs visible-sm" href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>">
<img id="product-collection-image-<?php echo $_product->getId(); ?>" src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(210,150); ?>" alt="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" />
</a>
<a class="product-image visible-md visible-lg" href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>">
<img id="product-collection-image-<?php echo $_product->getId(); ?>" src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(210,150); ?>" alt="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" />
</a>
<h2 class="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></h2>
<?php echo $this->getPriceHtml($_product, true) ?>
<?php
if ($this->getChild('name.after')) {
$_nameAfterChildren = $this->getChild('name.after')->getSortedChildren();
foreach ($_nameAfterChildren as $_nameAfterChildName) {
$_nameAfterChild = $this->getChild('name.after')->getChild($_nameAfterChildName);
$_nameAfterChild->setProduct($_product);
echo $_nameAfterChild->toHtml();
}
}
?>
</div>
<?php
$i++;
if ($i % 15 == 0) { echo "</div>"; }
?>
<?php endforeach; ?>
</div>
</div>
<?php else: ?>
<?php endif; ?>
</div>
</div>
$ _category是資源和第二個條件,你只有經過整數值。首先加載類別的數據,然後傳遞類別對象 – mjdevloper