|
BishopMartin - 01 April 2008 06:12 AM Unfortunately there is no tag added to identify the active page [ class="active" ] so I can not create a style class that only apply to the selected page. Other CMS systems I have used (Drupal for example) automatically add this tag if it notices that the current URL match the link URL.
Any one have any ideas how I can work around this problem, and add CSS specifically to the active page link? Any plans to add this feature/function in an upcoming release or fix?
Here is the solution I’ve found to add ‘class="active" to links li (just like first and last):
first copy Links.php from app/code/core/Mage/Page/Block/Template/ to app/code/local/Mage/Page/Block/Template/.
Edit the new file.
A new function is needed to get the requested URL. So add this new function inside the class:
/** * Get prepared requested URL * * @return string */ public function getPreparedRequestedUrl() { $requestedUrl = $this->getUrl(substr($this->getRequest()->getRequestUri(), 1)); return preg_replace('/(index\/|index\/index\/)$/', '', $requestedUrl); }
Edit your links template. By default, it’s app/design/frontend/.../.../template/page/template/links.php
by default the content is:
<?php $_links = $this->getLinks(); ?> <?php if(count($_links)>0): ?> <div> <ul<?php if($this->getName()): ?>):?> id="<?php echo $this->getName() ?>"<?php endif;?>> <?php foreach($_links as $_link): ?> <li <?php if($_link->getIsFirst()): ?> class="first"<?php elseif($_link->getIsLast()): ?> class="last"<?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() ?><?php echo $_link->getAfterText() ?></a></li> <?php endforeach; ?> </ul> </div> <?php endif; ?>
Replace with :
<?php $_links = $this->getLinks() ?> <?php // get the prepared requested url $_requestedUrl = $this->getPreparedRequestedUrl(); ?> <?php if(count($_links)>0): ?> <ul<?php if($this->getName()): ?>):?> id="<?php echo $this->getName() ?>"<?php endif;?> class="top-links"> <?php foreach($_links as $_link): ?> <?php // compare the requested url with the link url and add class="active" if matching ?> <li class="<?php echo $_requestedUrl == $_link->getUrl()? 'active ': '' ?><?php if($_link->getIsFirst()): ?>first<?php elseif($_link->getIsLast()): ?>last<?php endif; ?>" <?php echo $_link->getLiParams() ?>><a href="<?php echo $_link->getUrl() ?>" title="<?php echo $_link->getTitle() ?>" <?php echo $_link->getAParams() ?>><span></span><?php echo $_link->getBeforeText() ?><?php echo $_link->getLabel() ?><?php echo $_link->getAfterText() ?></a></li> <?php endforeach; ?> </ul> <?php endif; ?>
Now if you want to add a link to a CMS page (like home page), you can add links in the catalog.xml like that:
<reference name="top.links"> <!-- dont remove prepare = true --> <action method="addLink" translate="label title"><label>Home</label><url></url><title>Home</title><prepare>true</prepare><urlParams/><position>1</position></action> <action method="addLink" translate="label title"><label>test link</label><url>test_link</url><title>Test Link</title><prepare>true</prepare><urlParams/><position>2</position></action> </reference>
Tell me if that solution is working great for all your links.
|