2

Within topmenu.phtml I am removing the default so that I can easier create a custom menu with my own classes (for implementation into Foundation CSS Framework using Top Bar).

How would I add a custom "active" class to the parent <li> if a child / descendent category of the selected is currently active?

I'm using the following code to generate the menu:

<ul>
  <?php
       $obj = new Mage_Catalog_Block_Navigation();
       $storeCategories = $obj->getStoreCategories();
       Mage::registry('current_category') ? $currentCategoryId = Mage::registry('current_category')->getId() : $currentCategoryId='';
       foreach ($storeCategories as $_category):
   ?>
           <li class="<!--active class here if child is current -->">
               <a href="#"><?php echo $_category->getName(); ?></a>
               <?php $categoryChildren = $_category->getChildren(); ?>
               <?php if($categoryChildren->count()) : ?>
                   <ul class="dropdown">
                       <?php foreach($categoryChildren as $_categoryChild) : ?>
                           <?php $_categoryChildModel = Mage::getModel('catalog/category')->load($_categoryChild->getId());?>
                           <?php $categoryGrandchildren=$_categoryChild->getChildren(); ?>
                           <li>
                               <?php
                                   $currentCategoryId===$_categoryChild->getId() ? $bold="style=\"font-weight:bold\"" : $bold='';
                                   echo '&emsp;' . '<a href="' . $_categoryChildModel->getUrl() . '"' . $bold . '>' .  $_categoryChild->getName() . '</a>';
                               ?>
                           </li>
                           <?php if($categoryGrandchildren->count()) : ?>
                               <?php foreach($categoryGrandchildren as $_categoryGrandchild) : ?>
                                   <?php $_categoryGrandchildModel = Mage::getModel('catalog/category')->load($_categoryGrandchild->getId());?>
                                   <li>
                                       <?php
                                           $currentCategoryId===$_categoryChild->getId() ? $bold="style=\"font-weight:bold\"" : $bold='';
                                           echo '&emsp;&emsp;' . '<a href="' . $_categoryGrandchildModel->getUrl() . '"' . $bold . '>' .  $_categoryGrandchild->getName() . '(' . $_categoryGrandchildModel->getProductCount() . ')</a>';
                                       ?>
                                   </li>
                               <?php endforeach; ?>
                           <?php endif; ?>
                       <?php endforeach; ?>
                   </ul>
               <?php endif; ?>
           </li>
       <?php endforeach ?>

   </ul>

1 Answer 1

4

Change

<li class="<!--active class here if child is current -->">

To

<li<?php echo in_array($_category->getId(), Mage::registry('current_category')->getPathIds()) ? ' class="custom-active-class"' : '' ?>>

EDIT: You need to add the category id to the cache key to make it work. Otherwise this solution wouldn't work with block caching enabled.

12
  • guys, please comment whats wrong with the answer when you vote it down. The answer fully answers the main question, so I don't see a reason to minus it
    – alex_b
    Commented Oct 29, 2014 at 7:10
  • 1
    Your solution wouldn't work with block cache, Mage_Catalog_Block_Navigation is cached without an expiration time. "If cache_lifetime is equal to false, it means that the cache will never expire." => magentocommerce.com/wiki/5_-_modules_and_development/…
    – RobM84
    Commented Oct 29, 2014 at 8:17
  • 1
    Sure this would work, I added it to your answer.
    – RobM84
    Commented Oct 29, 2014 at 8:23
  • 1
    Good point. Glad to help :)
    – alex_b
    Commented Oct 29, 2014 at 22:19
  • 1
    Have you added category_id to cache key of the block?
    – alex_b
    Commented Oct 30, 2014 at 7:55

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.