2014-05-13 44 views
-5
<?php 

    $categoryid = 64; 

    $category = new Mage_Catalog_Model_Category(); 
    $category->load($categoryid); 
    $collection = $category->getProductCollection(); 
    $collection->addAttributeToSelect('*'); 

    foreach ($collection as $_product) { ?> 

    <a href="<?php echo $_product->getProductUrl() ?>"><img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(200); ?>" width="200" height="200" alt="" /></a> <a href="<?php echo $_product->getProductUrl(); ?>"><?php echo $_product->getName(); ?></a> 

<?php } ?> 
+1

這樣的問題不能在這裏得到解答。你必須確定你想要的東西。 – Slimshadddyyy

回答

-3
<?php 
foreach($this->getStoreCategories() as $_category) 
{ 
    $cur_category=Mage::getModel('catalog/category')->load($_category->getId()); 
    $collectionnn = Mage::getModel('catalog/product') 
           ->getCollection() 
           ->joinField('category_id','catalog/category_product', 'category_id', 'product_id = entity_id',null, 'left') 
           ->addAttributeToSelect('*') 
           ->addAttributeToFilter('category_id',array('in' => $cur_category->getId()));   
        foreach($collectionnn as $product) 
        { 
         echo "<a href=".$product->getProductUrl().">".$product->getName()."</a><br>"; 
         break; 
        } 
}    
?> 
+0

您使用了break;你不覺得它會對性能產生影響嗎?你也可以使用「LIMIT」。這也是不可取的 - > addAttributeToSelect('*') –

+0

如何在此代碼中添加限制。請告訴我 –

+0

@DushyantJoshi這怎麼可能是一個正確的答案? – Slimshadddyyy

0

嘗試以下

$category=Mage::getModel('catalog/category')->load($category_id); 
$products = $category->getProductCollection() 
//->addAttributeToSelect('*') // add all attributes - optional 
->addAttributeToSelect(array('name','entity_id','price','small_image','frontend_name')) 
->setPageSize(1) 
->setOrder('created_at', 'DESC'); //sets the order by created_at 
2

試試這個

$category = Mage::getModel('catalog/category')->load(64); 
$collection = $category->getProductCollection()->addAttributeToFilter('status', 1); 
$collection->getSelect()->limit(1); 
foreach($collection as $product) 
{ 
    echo "<a href=".$product->getProductUrl().">".$product->getName()."</a><br>"; 
} 
+1

從類別ID獲取單件產品意味着什麼? – Slimshadddyyy

+0

他只想顯示一個產品,該產品的類別爲id 64 –

+0

,哪個產品? – Slimshadddyyy