2010-08-19 77 views

回答

5

這給一試:

$currentCatIds = $_product->getCategoryIds(); 

$categoryCollection = Mage::getResourceModel('catalog/category_collection') 
    ->addAttributeToSelect('name') 
    ->addAttributeToSelect('url') 
    ->addAttributeToFilter('entity_id', $currentCatIds) 
    ->addIsActiveFilter(); 

乾杯, JD

+0

這將工作在1.4?我聽說category_ids屬性已被棄用... – silvo 2010-08-19 17:59:31

+0

我使用第一種方法,它在Magento 1.4.0.1上工作。 – a1anm 2010-08-22 09:55:15

+0

@silvo'category_ids'仍然出現在我的''eav_attribute'表中'attribute_id' = 91.您從哪裏聽說過關於棄用?我還沒有看到關於任何棄用的官方公告,這將是有趣的閱讀... – 2010-08-23 01:06:52

1

您可以使用下面的代碼以顯示與所選擇的產品的所有類別產品詳情頁面。

<?php $categories = $_product->getCategoryIds(); ?> 
      <?php foreach($categories as $k => $_category_id): ?> 
      <?php $_category = Mage::getModel('catalog/category')->load($_category_id) ?> 
      <a href="<?php echo $_category->getUrl() ?>"><?php echo $_category->getName() ?></a> 
      <?php endforeach; ?> 
+0

這很好用!但只有一件事,比如說我們不想在輸出中列出一個特定的類別ID(或幾個類別ID),請問這個代碼會如何實現? – 2013-09-06 10:40:29

0

該代碼將根據商店標識獲取產品類別名稱。這也將使用完整的多商店和多網站的概念

$product = Mage::getModel('catalog/product')->load($product_id); 
    $cats = $product->getCategoryIds(); 
    foreach ($cats as $category_id) { 
    $_cat = Mage::getModel('catalog/category')->setStoreId(Mage::app()- >getStore()->getId())->load($category_id); 
    echo $_cat->getName();    
    }