2012-03-15 118 views
0

我看了以前提出和回答螺紋:Magento display all categories on product view page with parent categoriesMagento的:顯示類別的產品屬於不同的產品頁面,但濾出一種特定的類別

與代碼:

$currentCatIds = $_product->getCategoryIds(); 
$categoryCollection = Mage::getResourceModel('catalog/category_collection') 
        ->addAttributeToSelect('name') 
        ->addAttributeToSelect('url') 
        ->addAttributeToFilter('entity_id', $currentCatIds) 
        ->addIsActiveFilter(); 
foreach($categoryCollection as $cat){ 
    echo $cat->getName().' '.$cat->getUrl(); 
} 

的代碼做添加類別鏈接到我的產品頁面,這是我想要的。

但我有一個名爲「默認類別」的特定類別,其他所有類別都在其下。無論如何,我可以過濾「默認分類」並將其隱藏在產品頁面中?

我在PHP很糟糕,所以請幫助我。

非常感謝您

馬庫斯

回答

0

嘗試增加類別級別的過濾器。 默認分類有水平= 1,孩子得+1

$categoryCollection = Mage::getResourceModel('catalog/category_collection') 
       ->addAttributeToSelect('name') 
       ->addAttributeToSelect('url') 
       ->addFieldToFilter('level', array('gteq' => 1)); 
       ->addAttributeToFilter('entity_id', $currentCatIds) 
       ->addIsActiveFilter(); 
0

如果您需要列出所有相關的產品視圖頁面所選產品的類別,你只需要把下面的代碼在app/design/frontend /// template/catalog/product/view.phtml

<?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; ?> 
相關問題