How to add active class in top links?Filter product collection for Criteo Tags in category page: module setup (config.xml)Top Menu cache problemHow to echo the configurable product price?Add active class to CMS page links in Top menu (static block links)Failing to add new attribute to orderMagento Top Links Login class disappears randomly!What is use of class names in base/default/template in magento?Add top links via extensionRedirect to external link from top links sectionMagento unable to access block class function from another phtml

Why can Carol Danvers change her suit colours in the first place?

Multiplicative persistence

When were female captains banned from Starfleet?

Electoral considerations aside, what are potential benefits, for the US, of policy changes proposed by the tweet recognizing Golan annexation?

What if a revenant (monster) gains fire resistance?

Creepy dinosaur pc game identification

Does malloc reserve more space while allocating memory?

A social experiment. What is the worst that can happen?

Are Captain Marvel's powers affected by Thanos' actions in Infinity War

Is aluminum electrical wire used on aircraft?

How to cover method return statement in Apex Class?

Biological Blimps: Propulsion

On a tidally locked planet, would time be quantized?

Temporarily disable WLAN internet access for children, but allow it for adults

How do you respond to a colleague from another team when they're wrongly expecting that you'll help them?

What should you do when eye contact makes your subordinate uncomfortable?

What should you do if you miss a job interview (deliberately)?

What exact color does ozone gas have?

Strong empirical falsification of quantum mechanics based on vacuum energy density

Angel of Condemnation - Exile creature with second ability

Can the US President recognize Israel’s sovereignty over the Golan Heights for the USA or does that need an act of Congress?

What does chmod -u do?

What happens if you are holding an Iron Flask with a demon inside and walk into an Antimagic Field?

Picking the different solutions to the time independent Schrodinger eqaution



How to add active class in top links?


Filter product collection for Criteo Tags in category page: module setup (config.xml)Top Menu cache problemHow to echo the configurable product price?Add active class to CMS page links in Top menu (static block links)Failing to add new attribute to orderMagento Top Links Login class disappears randomly!What is use of class names in base/default/template in magento?Add top links via extensionRedirect to external link from top links sectionMagento unable to access block class function from another phtml













1















I want to add active class in top links?




appdesignfrontenddefaultdefaulttemplatepagetemplatelinks.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; ?>









share|improve this question
















bumped to the homepage by Community 3 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.















  • on which condition you want to add active class

    – Bhupendra Jadeja
    Apr 17 '15 at 13:11















1















I want to add active class in top links?




appdesignfrontenddefaultdefaulttemplatepagetemplatelinks.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; ?>









share|improve this question
















bumped to the homepage by Community 3 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.















  • on which condition you want to add active class

    – Bhupendra Jadeja
    Apr 17 '15 at 13:11













1












1








1








I want to add active class in top links?




appdesignfrontenddefaultdefaulttemplatepagetemplatelinks.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; ?>









share|improve this question
















I want to add active class in top links?




appdesignfrontenddefaultdefaulttemplatepagetemplatelinks.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; ?>






magento-1.9 module custom






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 9 at 10:26









Satish Dubariya

315112




315112










asked Apr 17 '15 at 11:12









ravi patelravi patel

561418




561418





bumped to the homepage by Community 3 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.







bumped to the homepage by Community 3 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.














  • on which condition you want to add active class

    – Bhupendra Jadeja
    Apr 17 '15 at 13:11

















  • on which condition you want to add active class

    – Bhupendra Jadeja
    Apr 17 '15 at 13:11
















on which condition you want to add active class

– Bhupendra Jadeja
Apr 17 '15 at 13:11





on which condition you want to add active class

– Bhupendra Jadeja
Apr 17 '15 at 13:11










2 Answers
2






active

oldest

votes


















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; ?>





share|improve this answer























  • some right but some times 2 or more links active because of route-name or identifier is same for 2 links.

    – ravi patel
    Apr 17 '15 at 12:52











  • Can you give me an example of same route-name

    – Ahmed Elawadi
    Apr 17 '15 at 12:53











  • "customer" all ways display active class only in my account just see default magento top links.

    – ravi patel
    Apr 17 '15 at 12:57












  • Where I used this script I didn't have any links to customer pages, not sure if getActionName() will work.

    – Ahmed Elawadi
    Apr 17 '15 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.






share|improve this answer






















    Your Answer








    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "479"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f63959%2fhow-to-add-active-class-in-top-links%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    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; ?>





    share|improve this answer























    • some right but some times 2 or more links active because of route-name or identifier is same for 2 links.

      – ravi patel
      Apr 17 '15 at 12:52











    • Can you give me an example of same route-name

      – Ahmed Elawadi
      Apr 17 '15 at 12:53











    • "customer" all ways display active class only in my account just see default magento top links.

      – ravi patel
      Apr 17 '15 at 12:57












    • Where I used this script I didn't have any links to customer pages, not sure if getActionName() will work.

      – Ahmed Elawadi
      Apr 17 '15 at 13:08
















    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; ?>





    share|improve this answer























    • some right but some times 2 or more links active because of route-name or identifier is same for 2 links.

      – ravi patel
      Apr 17 '15 at 12:52











    • Can you give me an example of same route-name

      – Ahmed Elawadi
      Apr 17 '15 at 12:53











    • "customer" all ways display active class only in my account just see default magento top links.

      – ravi patel
      Apr 17 '15 at 12:57












    • Where I used this script I didn't have any links to customer pages, not sure if getActionName() will work.

      – Ahmed Elawadi
      Apr 17 '15 at 13:08














    0












    0








    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; ?>





    share|improve this answer













    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; ?>






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Apr 17 '15 at 11:24









    Ahmed ElawadiAhmed Elawadi

    1,438720




    1,438720












    • some right but some times 2 or more links active because of route-name or identifier is same for 2 links.

      – ravi patel
      Apr 17 '15 at 12:52











    • Can you give me an example of same route-name

      – Ahmed Elawadi
      Apr 17 '15 at 12:53











    • "customer" all ways display active class only in my account just see default magento top links.

      – ravi patel
      Apr 17 '15 at 12:57












    • Where I used this script I didn't have any links to customer pages, not sure if getActionName() will work.

      – Ahmed Elawadi
      Apr 17 '15 at 13:08


















    • some right but some times 2 or more links active because of route-name or identifier is same for 2 links.

      – ravi patel
      Apr 17 '15 at 12:52











    • Can you give me an example of same route-name

      – Ahmed Elawadi
      Apr 17 '15 at 12:53











    • "customer" all ways display active class only in my account just see default magento top links.

      – ravi patel
      Apr 17 '15 at 12:57












    • Where I used this script I didn't have any links to customer pages, not sure if getActionName() will work.

      – Ahmed Elawadi
      Apr 17 '15 at 13:08

















    some right but some times 2 or more links active because of route-name or identifier is same for 2 links.

    – ravi patel
    Apr 17 '15 at 12:52





    some right but some times 2 or more links active because of route-name or identifier is same for 2 links.

    – ravi patel
    Apr 17 '15 at 12:52













    Can you give me an example of same route-name

    – Ahmed Elawadi
    Apr 17 '15 at 12:53





    Can you give me an example of same route-name

    – Ahmed Elawadi
    Apr 17 '15 at 12:53













    "customer" all ways display active class only in my account just see default magento top links.

    – ravi patel
    Apr 17 '15 at 12:57






    "customer" all ways display active class only in my account just see default magento top links.

    – ravi patel
    Apr 17 '15 at 12:57














    Where I used this script I didn't have any links to customer pages, not sure if getActionName() will work.

    – Ahmed Elawadi
    Apr 17 '15 at 13:08






    Where I used this script I didn't have any links to customer pages, not sure if getActionName() will work.

    – Ahmed Elawadi
    Apr 17 '15 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.






    share|improve this answer



























      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.






      share|improve this answer

























        0












        0








        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.






        share|improve this answer













        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 6 '15 at 19:46









        Vicky DevVicky Dev

        1,27761539




        1,27761539



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Magento Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f63959%2fhow-to-add-active-class-in-top-links%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Disable / Remove link to Product Items in Cart Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Announcing the arrival of Valued Associate #679: Cesar Manara Unicorn Meta Zoo #1: Why another podcast?How can I limit products that can be bought / added to cart?Remove item from cartHide “Add to Cart” button if specific products are already in cart“Prettifying” the custom options in cart pageCreate link in cart sidebar to view all added items After limit reachedLink products together in checkout/cartHow to Get product from cart and add it againHide action-edit on cart page if simple productRemoving Cart items - ObserverRemove wishlist items when added to cart

            Helsingin valtaus Sisällysluettelo Taustaa | Yleistä sotatoimista | Osapuolet | Taistelut Helsingin ympäristössä | Punaisten antautumissuunnitelma | Taistelujen kulku Helsingissä | Valtauksen jälkeen | Tappiot | Muistaminen | Kirjallisuutta | Lähteet | Aiheesta muualla | NavigointivalikkoTeoksen verkkoversioTeoksen verkkoversioGoogle BooksSisällissota Helsingissä päättyi tasan 95 vuotta sittenSaksalaisten ylivoima jyräsi punaisen HelsinginSuomalaiset kuvaavat sotien jälkiä kaupungeissa – katso kuvat ja tarinat tutuilta kulmiltaHelsingin valtaus 90 vuotta sittenSaksalaiset valtasivat HelsinginHyökkäys HelsinkiinHelsingin valtaus 12.–13.4. 1918Saksalaiset käyttivät ihmiskilpiä Helsingin valtauksessa 1918Teoksen verkkoversioTeoksen verkkoversioSaksalaiset hyökkäävät Etelä-SuomeenTaistelut LeppävaarassaSotilaat ja taistelutLeppävaara 1918 huhtikuussa. KapinatarinaHelsingin taistelut 1918Saksalaisten voitonparaati HelsingissäHelsingin valtausta juhlittiinSaksalaisten Helsinki vuonna 1918Helsingin taistelussa kaatuneet valkokaartilaisetHelsinkiin haudatut taisteluissa kaatuneet punaiset12.4.1918 Helsingin valtauksessa saksalaiset apujoukot vapauttavat kaupunginVapaussodan muistomerkkejä Helsingissä ja pääkaupunkiseudullaCrescendo / Vuoden 1918 Kansalaissodan uhrien muistomerkkim

            Adjektiivitarina Tarinan tekeminen | Esimerkki: ennen | Esimerkki: jälkeen | Navigointivalikko