2

I want to add active class in top links?

\app\design\frontend\default\default\template\page\template\links.phtml

<?php $_links = $this->getLinks(); ?>
<?php if (count($_links) > 0): ?>
    <ul class="links"<?php if ($this->getName()): ?> id="<?php echo $this->getName() ?>"<?php endif; ?>>
        <?php foreach ($_links as $_link): ?>
            <?php if ($_link instanceof Mage_Core_Block_Abstract): ?>
                <?php echo $_link->toHtml() ?>
            <?php else: ?>
                <li<?php if ($_link->getIsFirst() || $_link->getIsLast()): ?> class="<?php if ($_link->getIsFirst()): ?>first<?php endif; ?><?php if ($_link->getIsLast()): ?> last<?php endif; ?>"<?php endif; ?> <?php echo $_link->getLiParams() ?>><?php echo $_link->getBeforeText() ?><a href="<?php echo $_link->getUrl() ?>" title="<?php echo $_link->getTitle() ?>" <?php echo $_link->getAParams() ?>><?php echo $_link->getLabel() ?></a><?php echo $_link->getAfterText() ?></li>
                <?php endif; ?>
            <?php endforeach; ?>
    </ul>
<?php endif; ?>
1
  • on which condition you want to add active class Apr 17, 2015 at 13:11

2 Answers 2

0

what about:

<?php $_links = $this->getLinks(); ?>
<?php 
// active states
// using $_link->getBeforeText() against the RouteName or Idenifier ti get active states
$routeName = Mage::app()->getRequest()->getRouteName();
$identifier = Mage::getSingleton('cms/page')->getIdentifier();
if ($identifier ==null): 
$identifier = $routeName; 
endif;
?>
<?php if(count($_links)>0): ?>
<ul class="links"<?php if($this->getName()): ?> id="<?php echo $this->getName() ?>"<?php endif;?>>
    <?php foreach($_links as $_link): ?>
        <?php if ($_link instanceof Mage_Core_Block_Abstract):?>
            <?php echo $_link->toHtml() ?>
        <?php else: ?>
            <li class="<?php echo $_link->getBeforeText(); if( $identifier == $_link->getBeforeText()): echo " active"; endif; ?> <?php if($_link->getIsFirst()||$_link->getIsLast()): ?><?php if($_link->getIsFirst()): ?>first<?php endif; ?><?php if($_link->getIsLast()): ?> last<?php endif; ?><?php endif; ?>" <?php echo $_link->getLiParams() ?>><?php // echo $_link->getBeforeText() ?><a href="<?php echo $_link->getUrl() ?>" title="<?php echo $_link->getTitle() ?>" <?php echo $_link->getAParams() ?>><?php echo $_link->getLabel() ?></a><?php echo $_link->getAfterText() ?></li>
        <?php endif;?>
    <?php endforeach; ?>
</ul>

<?php endif; ?>
4
  • some right but some times 2 or more links active because of route-name or identifier is same for 2 links. Apr 17, 2015 at 12:52
  • Can you give me an example of same route-name Apr 17, 2015 at 12:53
  • "customer" all ways display active class only in my account just see default magento top links. Apr 17, 2015 at 12:57
  • Where I used this script I didn't have any links to customer pages, not sure if getActionName() will work. Apr 17, 2015 at 13:08
0

A better and more cleaner and also simple approach would be to compare every link's url with the current url and if matches then add "active" class to it.

<?php $_links = $this->getLinks(); ?>
<?php $currentUrl = Mage::helper('core/url')->getCurrentUrl(); ?>

<?php if (count($_links) > 0): ?>
    <ul class="links"<?php if ($this->getName()): ?> id="<?php echo $this->getName() ?>"<?php endif; ?>>
        <?php foreach ($_links as $_link): ?>
            <?php if ($_link instanceof Mage_Core_Block_Abstract): ?>
                <?php echo $_link->toHtml() ?>
            <?php else: ?>
                <li<?php if ($_link->getIsFirst() || $_link->getIsLast()): ?> class="<?php if ($_link->getIsFirst()): ?>first<?php endif; ?><?php if ($_link->getIsLast()): ?> last<?php endif; ?><?php if($_link->getUrl() == $currentUrl): ?> active<?php endif; ?>"<?php endif; ?> <?php echo $_link->getLiParams() ?>><?php echo $_link->getBeforeText() ?><a href="<?php echo $_link->getUrl() ?>" title="<?php echo $_link->getTitle() ?>" <?php echo $_link->getAParams() ?>><?php echo $_link->getLabel() ?></a><?php echo $_link->getAfterText() ?></li>
                <?php endif; ?>
            <?php endforeach; ?>
    </ul>
<?php endif; ?>

But the urls if same shouldn't have even the difference of last slash or this condition won't work, so you might want to use rtrim($url, '/'); for removing last trailing slash.

Hope this helps.

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.