0
votes

I have read the previously asked and answered thread: Magento display all categories on product view page with parent categories

with the code:

$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();
}

The code does add the category links to my product page, which is what I want.

But I have one specific category named "Default Category", and all other categories are under it. Is there anyway I can filter the "Default Category" and hide it from the product page?

I am terrible at PHP so please help me.

Thank you very much

Marcus

2

2 Answers

0
votes

Try adding category level filter. Default Category have level = 1, children gets +1

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

If you need to list all the categories related to the selected product in the product view page, you just need to put the following code in the 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; ?>