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)custom layout not rendering for overridden login methodOverwrite not working as expectedMagento Top Links Login class disappears randomly!Add top links via extensionError Call to a member function getDescription()Magento unable to access block class function from another phtml
Reasons for using monsters as bioweapons
Derivative is just speed of change?
How can flights operated by the same company have such different prices when marketed by another?
Being told my "network" isn't PCI Complaint. I don't even have a server! Do I have to comply?
Python π = 1 + (1/2) + (1/3) + (1/4) - (1/5) + (1/6) + (1/7) + (1/8) + (1/9) - (1/10) ...1748 Euler
How to trick a fairly simplistic kill-counter?
Should students have access to past exams or an exam bank?
Is it moral to remove/hide certain parts of a photo, as a photographer?
What is the significance of $(logname)?
How do I find SFDX CLI default installation folder on Mac?
How do Canadians get a visa to go to Saudi Arabia?
Word for giving preference to the oldest child
Is there anyway to harden soft braised carrots?
Should I put my name first or last in the team members list?
How did Biff return to 2015 from 1955 without a lightning strike?
How to avoid a lengthy conversation with someone from the neighborhood I don't share interests with
How does Asimov's second law deal with contradictory orders from different people?
Is this mechanically safe?
Not taking Bereavement Leave
Is the EU really banning "toxic propellants" in 2020? How is that going to work?
Can the additional attack from a Samurai's Rapid Strike have advantage?
A game of red and black
Oath of redemption: Does Emmissary of Peace reflect damage taken from Aura of the Guardian?
Just how much information should you share with a former client?
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)custom layout not rendering for overridden login methodOverwrite not working as expectedMagento Top Links Login class disappears randomly!Add top links via extensionError Call to a member function getDescription()Magento unable to access block class function from another phtml
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
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
add a comment |
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
on which condition you want to add active class
– Bhupendra Jadeja
Apr 17 '15 at 13:11
add a comment |
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
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
magento-1.9 module custom
edited Feb 9 at 10:26
Satish Dubariya
4841 silver badge12 bronze badges
4841 silver badge12 bronze badges
asked Apr 17 '15 at 11:12
ravi patelravi patel
5764 silver badges18 bronze badges
5764 silver badges18 bronze badges
on which condition you want to add active class
– Bhupendra Jadeja
Apr 17 '15 at 13:11
add a comment |
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
add a comment |
2 Answers
2
active
oldest
votes
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; ?>
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
add a comment |
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.
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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; ?>
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
add a comment |
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; ?>
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
add a comment |
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; ?>
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; ?>
answered Apr 17 '15 at 11:24
Ahmed ElawadiAhmed Elawadi
1,4287 silver badges20 bronze badges
1,4287 silver badges20 bronze badges
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
add a comment |
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
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 6 '15 at 19:46
Vicky DevVicky Dev
1,3127 gold badges18 silver badges40 bronze badges
1,3127 gold badges18 silver badges40 bronze badges
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
on which condition you want to add active class
– Bhupendra Jadeja
Apr 17 '15 at 13:11