I got a list of categories that show on a product view page. The list is shown as a few top categories with their sub categories under them. This works, except it, now shows all the categories from my entire website, not only the categories, the current product belong too.

How can I fix that?

My code:

 $categoryCollection = Mage::getResourceModel('catalog/category_collection')
  // ->addFieldToFilter('id', array('nin' => 128))
  $out = "<ul>";
  foreach($categoryCollection as $cat){

      $out .= "<li>";
      $out .= "<b><a href='".$cat->getUrl()."'>".$cat->getName()."</a></b>";
      $out .="<ul class='sub'>";
      $children = Mage::getModel('catalog/category')->load($cat->getId())->getChildrenCategories();
          foreach($children as $child){
              $out .="<li><a href='".$child->getUrl()."'>".$child->getName()."</a></li>";
          $out .="</ul>";
      $out .= "</li>";
  $out .= "</ul>";
  echo $out;

This shows ALL top categories from my website with ALL their subcategories.

I only want to show the categories that the product belong too.

I got it to work but this doesn't show them in the list style I want, it just shows them all in one big list, not sorted by their parent categories. (with this code:)

  // Haal alle categorieen op waar het product onder valt
    $currentCatIds = $_product->getCategoryIds();
    $categoryCollection = Mage::getResourceModel('catalog/category_collection')
     ->addAttributeToFilter('entity_id', $currentCatIds)

  //  echo '<pre>';
  //  print_r($categoryCollection);
  //  echo '</pre>';

         $sorted = sort($categoryCollection);

   $catlist .= '<ul class="categorielijst">';
         foreach($categoryCollection as $cat){
     if($cat->getName() != 'Root'){
       $catlist .= '
                <li><a href="'.$cat->getUrl().'">'.$cat->getName().'</a></li>';
    $catlist .= '</ul>';
    echo $catlist;

1 Answer 1


So let me get this right you only want to show the categories a product belongs to? if so please use the following to get that result

$productCategories = $_product->getCategoryIds();
$categoryCollection = Mage::getResourceModel('catalog/category_collection')
                     ->addAttributeToFilter('entity_id', $productCategories)

    foreach($categoryCollection as $cat){
      echo $cat->getName().' '.$cat->getUrl();

If you needed something else please post clarification if the form of a comment thanks

  • Works great on Category list page but breaks the search results page ! any idea ?
    – Devtype
    Sep 18, 2017 at 11:31
  • Yes an if(!empty($productCategories){ after $productCategories and after the closing foreach } Sep 18, 2017 at 19:43
  • Thanks for this, however I need to show the category names etc on search results, please see here magento.stackexchange.com/questions/193633/…
    – Devtype
    Sep 19, 2017 at 12:00
  • This is not tested but you will need to edit the getProductCollection function in Mage_CatalogSearch_Model_Advanced but remember to copy to local do not edit the core Sep 19, 2017 at 13:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.