Madproject

Always know where your towel is.

Magento 1.6 Menu Link Active State

Ahh Magento. So powerful but what a pain in the ass when you don’t work with it regularly.

So you’ve added custom menu items to your top.phtml

app/design/frontend/default/<strong>theme_name</strong>/template/navigation/top.phtml

It all works great but the .active class isn’t being applied to the menu item when you’re on said page.

For the home link

    <li class="home <?php echo (Mage::helper('core/url')->getCurrentUrl() === Mage::helper('core/url')->getHomeUrl()) ? "active" : ""; ?>"><a href="<?php echo $this->getUrl('')?>"><?php echo $this->__('Home') ?></a></li>

Note: What we’re doing here is we’re using built-in Magento methods and comparing the current url with the home url. If they’re the same, we append the active class to whatever other class we might have.

For any other link

    <li class="the-story <?php echo (strstr(Mage::helper('core/url')->getCurrentUrl(),"URL KEY HERE")) ? "active" : ""; ?>"><a href="<?php echo $this->getUrl('the-story')?>"><?php echo $this->__('The Story') ?></a></li>

Note: Similar to the home link code but this time we use the php function strstr() to see if the current page URL contains the URL key of the menu item page. If it does that mean we’re on that particular page so we append the .active class, same as above.

Hope this helps!

5 comments for “Magento 1.6 Menu Link Active State

  1. March 12, 2012 at 04:48

    Hi
    The above code does not work when caching is turned on. If you disable caching, it works fine.

    • Trev
      March 12, 2012 at 10:49

      Thanks for the input. I’ll have a look into it. Do you have a solution yourself?

  2. June 24, 2013 at 06:58

    Trev! I wonder Does that work in Mega Menu?

    • October 21, 2014 at 12:42

      Hello, I need some help with it. When I try to go to magento front page it pops up this: Warning: simempxll_load_string(): Entity: line 2: parser error : Comment not terminated. Nothing is shown in system.log about this as well. I don’t know where to put Mage::log(print_r($filename, true)); to help me find this error.Help please.

  3. July 22, 2014 at 00:03

    What should I put in the URL KEY HERE? Is this right?

    domain.com/products/my-current-category.html

    URL KEY HERE = /products/my-current-category.html

    Is that the right key? Or do I need to use the complete URL?

Leave a Reply

Your email address will not be published. Required fields are marked *