Blog

Editing Magento's Top Links (The Better Way)

You might need to read the post about using a local.xml file before this post makes much sense.

We offer all our clients a completely customized design from scratch, which means changing anything - including those annoying defaulted top links.  This post will show you how to edit your top links (without editing core layout files which may change when you update Magento) by utilizing a local.xml file. You can even add your own custom links without touching any template files! [NOTE: This specific example assumes you are using the blank theme. Layout handles may differ from theme to theme.]

ADDITION: Check out the blog post about editing Magento's footer links

In local.xml:

<?xml version="1.0"?>
<layout version="0.1.0">
    <default>
        <reference name="root">
            <reference name="top.links">
                <!-- Add custom links. Pretty self-explanatory.
                Dig into app/code/core/Mage/Page/Block/Template/Links.php for more info -->
                <action method="addLink" translate="label title">
                    <label>About Us</label>
                    <url>about</url>  <!-- can use full url also -->
                    <title>About Us</title>
                    <prepare>true</prepare> <!-- set true if adding base url param -->
                    <urlParams helper="core/url/getHomeUrl"/> <!-- base url - thanks @Russ! -->
                    <!-- there are a few param you can send to do different things in <urlParams> 
                             dig into app/code/core/Mage/Core/Model/Url.php, around line 803 -->                   
 
                    <!-- below adds #add-fragment to the end of your url -->
                    <!-- <urlParams><_fragment>add-fragment</_fragment></urlParams> -->
 
                    <!-- below adds ?add-query to the end of your url -->
                    <!-- <urlParams><_query>add-fragment</_query></urlParams> -->
 
                    <!-- below gives you a new session id (i think...)-->
                    <!-- <urlParams><_nosid>true</_nosid></urlParams> -->
 
                    <!-- below replaces double quotes, single quotes, greater than, and less than signs 
                             to their respective url escaped replacements (%22, %27, %3E, %3C) -->
                    <!-- <urlParams><_escape>i'm-a-blog-url</_escape></urlParams> -->
 
                    <position>1</position>
                    <liParams/>
                    <aParams>class="top-link-about-us"</aParams>
                    <beforeText></beforeText>
                    <afterText></afterText>
                </action>
 
                <!-- Removes 'My Account' link - Default position: 10 -->
                <action method="removeLinkByUrl"><url helper="customer/getAccountUrl"/></action>
 
                <!-- Removes 'Wishlist' link - Default position: 20 -->
                <!-- for Magento 1.3.x -->
                <action method="removeLinkByUrl"><url helper="wishlist/"/></action>
 
                <!-- for Magento 1.4.x -->
                <remove name="wishlist_link"/>
 
                <!-- Removes 'My Cart' AND 'Checkout' links
                Default position: 40 and 50 respectively -->
                <remove name="checkout_cart_link"/>
 
                <!-- To re-add 'My Cart' or 'Checkout' after removing both -->
                <block type="checkout/links" name="checkout_cart_link_custom">
                    <action method="addCartLink"></action>
                    <action method="addCheckoutLink"></action>
                </block>
            </reference>
        </reference>
    </default>
 
    <customer_logged_out>
        <!-- Removes 'Log In' link - Default position: 60 -->
        <reference name="top.links">
            <action method="removeLinkByUrl"><url helper="customer/getLoginUrl"/></action>
        </reference>
    </customer_logged_out>
 
    <customer_logged_in>
        <!-- Removes 'Log Out' link - Default position: 60 -->
        <reference name="top.links">
            <action method="removeLinkByUrl"><url helper="customer/getLogoutUrl"/></action>
        </reference>
    </customer_logged_in>
 
</layout>

If you absolutely cannot find a way to customize your top links using these methods, you can edit the /template/page/template/links.phtml

Posted on February 24, 2010

Posted by Kevin Kirchner

Comments

Matt's picture

Thanks for this article. It

Thanks for this article. It was very helpful. I ended up removing the cart and checkout links, added the cart link back, and then created a custom link to replace the checkout link and have it pointing to the cart page. Turned out to be a nice quick fix to make sure customers end up on the cart page to see if any items are on backorder, and so they see where they are supposed to enter any discount codes. Modifying the template file for this wouldn't have been quite as simple I expect.

Toni Moya's picture

I want remove top.link into

I want remove top.link into onepage checkout, I've tried several ways but I can not.

Josh Ribakoff's picture

Why do you feel that adding a

Why do you feel that adding a link via layout XML is better than adding it a .phtml file? Given that both can be done in a way that isn't overwritten on upgrade, I'd prefer to edit the .phtml file, since its raw HTML & PHP, which is better documented than Magento's proprietary layout XML (which is not only not documented well, but changes from version to version). Why do you recommend editing the layout XML given these issues?

Mike's picture

I've been trying to figure

I've been trying to figure out how to add a non-CMS page link to the Log in | My Cart | My Account, etc. menu. I see here a lot of help adding links WITHIN the CMS, but I want to add a HOME link to my "outside" home page, not the Magento CMS home page. Are there any suggestions for that?

I would also like to do the same for the page logo, but that's another topic discussion.

Thanks.

Mike

Darren Felton's picture

Top Link for every page but home page

Go ahead and add your link, with the default layout handle. Then remove it just on the home page using the home page's layout handle. The layout handle for the home page is always "cms_index_index".

Magento Ajax Add to Cart's picture

Hi, Also I found a way to

Hi, Also I found a way to remove the "add to comparison" link/button in product page. Hope this is also to be included in your tips.

Here the mentioned: http://php.quicoto.com/how-to-remove-add-to-compare-magento

thedotwriter's picture

Oops, seems like there's a

Oops, seems like there's a issue with formatting options. I was talking about: reference name="root".

thedotwriter's picture

Why the use of ? Is that

Why the use of ? Is that really necessary?

Jeremy's picture

Hi all. I know less than

Hi all. I know less than zero about all this coding stuff. I (my web designer) is having an issue with spacing in the header links. The links are "Login | My Account | My Cart | Checkout". The spacing between the dashes ( | ) and links are not consistant across the web browsers. They are not evenly spaced on any browsers and look outright wrong on others (Safari, Mac OS, IE W7 & Mac). Do you guys and gals have any information I can pass along to my web designer?

Thanks

Colin Austin's picture

Thanks a lot for this helpful

Thanks a lot for this helpful and technically accurate article. Great to see you using local.xm unlike so many of the attempts that are out there that hack away at core files. Works nicely for me in 1.6.

ok2phone's picture

My usual ways to go out this

My usual ways to go out this problem is that:
add some code in page.xml as following:

upheader

and then in 3columns.xml
<?php echo $this->getChildHtml('upheader') ?>

That's work well for me!

http://www.ok2phone.com

Diana's picture

Hello, thanks for the help

Hello, thanks for the help with this. :)
I have a question about other links, not top links...

I need the wishlist to be usable for logged in users only.
so the link 'add to wishlist' on the products page should just be visible when a customer is logged in.

Any ideas?

Thanks
Diana

Anonymous's picture

Disable it from the

Disable it from the SYSTEM-CONFIGURATIONS-ADVANCE-Mage_Wishlist

Magent's picture

Thanks for you share buddy! I

Thanks for you share buddy! I have a question, I can't add a new class attribute with your code. A id or something else worked perfectly. Would you like to tell where is wrong?

Magento Commerce's picture

Hello everybody I have got 3

Hello everybody
I have got 3 blogs and 2 websites.Can i have google adsense ads in my all of these blogs and websites ? Whether i have to apply for different google adsense accounts or a single account will work for all these websites and blogs.

Thank You.

Andrei's picture

Greate resource. Thanks very

Greate resource. Thanks very much :D

Expert Magento's picture

thanks for the information.

thanks for the information. It is actually very helpful to the ones who are using magento for their online stores.

Daniel's picture

third try: er code messed

third try:

er code messed up...

HowTo Remove WishlistLink:
Workaround: http://stackoverflow.com/questions/4423631/magento-removing-wishlist-lin...

HowTo Set CSS ID in ul tag:


<reference name="top.links">
<action method="setData"><key>name</key><value>main_nav</value></action>
....
</reference>

Daniel's picture

er code messed up... HowTo

er code messed up...

HowTo Remove WishlistLink:
Workaround: http://stackoverflow.com/questions/4423631/magento-removing-wishlist-lin...

HowTo Set CSS ID in ul tag:

<code>

namemain_nav
....

</code>

Daniel's picture

HowTo Remove

HowTo Remove WishlistLink:
Workaround: http://stackoverflow.com/questions/4423631/magento-removing-wishlist-lin...

blank-link.phtml

HowTo Set CSS ID in ul tag:

namemain_nav
....

Calvin's picture

Er, the code didn't show up,

Er, the code didn't show up, how do I post code :P

Calvin's picture

Andy, I'm running 1.4.2.0 as

Andy, I'm running 1.4.2.0 as well and this method (that I found another forum) for knocking out the wishlist worked for me:

Comment out the following from frontend/default/yourtheme/layout/wishlist.xml. Just make sure your cache is disabled and such.

<!--

-->

Really wish there was a less-complicated way to do all this...

mau's picture

Wishlist removal code does

Wishlist removal code does not work anymore with 1.5, I just deactivate it in control panel ´cause I won´t need it.

Jameson Proctor's picture

@Jameson Proctor I figured

@Jameson Proctor

I figured it out <prepare>true</prepare> needs to be set to <prepare /> not <prepare>false</prepare>. For some reason, prepare to false is no different than setting it to true.

Jameson Proctor's picture

This an excellent tutorial.

This an excellent tutorial. Thank you. I am trying to use this to add a link to the top.links that points to another domain. So far I have had no luck. No matter what I do, Magento just appends the link to the end of my base url.

Any help would be greatly appreciated.

andy's picture

running 1.4.2.0 and the wish

running 1.4.2.0 and the wish code still breaks the site. any ideas? what am i doing wrong?

Also trying to hard code adding a navigation link at top.phml. when I edited the code the main-container area is broke. Any help you can give is greatly appreciated.

Thanks

Mizpah's picture

Heya kkircher, (firstly,

Heya kkircher,

(firstly, apologies for the miss-post above, please delete!)

Is there any additional magic to making this work with the category links instead of top.links ?

I am trying the below in catalog.xml to start with prior to trying to moving it into local once its working (local I assume has an unset issue as it causes an exception!) ?

<reference name="top.menu">
<block type="catalog/navigation" name="catalog.topnav" template="catalog/navigation/top.phtml">
<action method="addLink" translate="label title">
<label>Home Page</label>
<url helper="core/url/getHomeUrl"/>
<title>Home Page</title>
<prepare/>
<urlParams/>
<position>1</position>
</block>
</action>
</block>
</reference>

I assumed the above would work (1.5.0.1, blank theme), but in effect nothing gets output. Suggestions welcome!

alaba's picture

great , it help me ,too. TKS.

great , it help me ,too. TKS. ( in ver.1.4 )

oserk's picture

Home
/
Home
true

0

class="top-link-about-us"

oserk's picture

Great reading! Makes

Great reading!

Makes magento to customize way simpler! I'm trying to add front page to my navigation, but it just dont work. I did managed to add it to top.links. Here is my code, magento 1.4.2.0

<PRE>

Home
/
Home
true

0

class="top-link-about-us"

</PRE>

Jay's picture

Such a good system is the old

Such a good system is the old Magento, but for the fact that there is no decent way to manage those frickin menus! menu items should be pulled from the database as proper menu items, that are linked to "menus" which are also stored in the database, then, only once that is working OK should they add in the ability to override (via XML) full menus, ir single menu items. I think that is a good balance between "developer" friendly, and "commercial self preservation". I.e. only savvy developers can work it out, apart from the people that make money at Magento - I believe this is their business model in the big picture, I dont blame them either :)

Jef Gunion's picture

Whoa! Very deep! I'll give

Whoa! Very deep! I'll give this implementation a try and let you know how it goes. Thank you very much for the response and idea!
Jef

kkirchner's picture

@Jef - Very interesting

@Jef - Very interesting question. I dug in to catalog.xml and catalog/product/compare/sidebar.phtml to see what I could find.

The button that opens the comparing pop-up box uses a php method to dynamically create the pop-up box. So you can probably use your own phtml file for it.

In local.xml:

Now create the comparelink.phtml file and modify the code from catalog/product/compare/sidebar.phtml:

<?php if($this->helper('catalog/product_compare')->getItemCount()>0): ?>
__('Compare') ?>" onclick="popWin('<?php echo $this->helper('catalog/product_compare')->getListUrl() ?>','compare','top:0,left:0,width=820,height=600,resizable=yes,scrollbars=yes')"><?php echo $this->__('Compare') ?>
<?php endif; ?>

I've not tested this but it should work in theory... good luck!

Jef Gunion's picture

Thanks for these great

Thanks for these great alternatives!!

I have a new question for you. I'm using a custom theme and a one column format throughout the entire site. My problem is that when users wish to click on "Add to Compare" for several products, the corresponding link to "Compare Products" is not available anywhere. I wanted that link to automatically be available in the top links discussed above. In the 2 or 3 column format, the "Compare Products" list and link is in the sidebar for immediate access.
Any thoughts on this dilemma???

Thanks again!!
Jef

Support Your Sound's picture

Thanks, I've been struggling

Thanks, I've been struggling with this for a while! Top menu links now edited!

Mark's picture

Bravo! The top links were

Bravo! The top links were doing my head in... it's nice to have them in one place! Thanks for the info.

vishal lakhani's picture

HI I WANT TO remove totally

HI I WANT TO remove totally menu and add new html as topnew.phtml i have done like this in local.xml

its removing top.phtml but i cant add topnew.pthml Please help me…Can anyone??

Waiting for reply

vishal lakhani's picture

HI I WANT TO remove totally

HI I WANT TO remove totally menu and add new html as topnew.phtml i have done like this in local.xml

its removing top.phtml but i cant add topnew.pthml Please help me...

Waiting for reply

Lego's picture

Thanks, couldn't quite get

Thanks, couldn't quite get those darn links removed with the other solutions. This one finally seemed to work.

Deko Web's picture

Was an expression of thanks

Was an expression of thanks really useful.

Alessandro Ronchi's picture

Hi, pay attention when you

Hi,
pay attention when you specify a “false” value: leave the tag empty (ie: <prepare/>) or use 0 as false (ie: <prepare>0</prepare>) otherwise you won’t get what you expect.

Thank you for your help.
Sincerely, Alessandro

Alessandro Ronchi's picture

Hi, pay attention when you

Hi,
pay attention when you specify a "false" value: leave the tag empty (ie: ) or use 0 as false (ie: 0) otherwise you won't get what you expect.

Thank you for your help.
Sincerely, Alessandro

kkirchner's picture

@Yasmine - The code goes in a

@Yasmine - The code goes in a local.xml file. To learn about where to put the local.xml file, take a look at our blog post here.

Thanks!

yasmine's picture

I wanna know where i should

I wanna know where i should place the code showing above ...Thanks for help

Brandon's picture

@ehansen, Thanks for the

@ehansen,
Thanks for the tip. I'll give that a shot and see how it goes.

ehansen's picture

@Brandon - The simplest

@Brandon - The simplest solution for making changes like that is to use the Translate Inline tool. You can turn on this tool on the System > Configuration > Developer page.

Brandon's picture

New to Magento development

New to Magento development here. Is there an easy way to edit the text of My WishList and My Account this way? I want to edit My Wishlist to simply say "Wishlist". I would like to edit My Account to say "Sign In" (only when a user isn't currently signed in).

kkirchner's picture

Another option - you can copy

Another option - you can copy the removeLinkByUrl method in app/code/core/Mage/Page/Block/Template/Links.php and paste it in a custom module that extends the Mage_Customer_Block_Account_Navigation class in app/code/core/Mage/Customer/Block/Account/Navigation.php. If you're going to go that far, you might as well add a custom helper to get the tag and review path with your baseUrl. Check out this comment.

Vier's picture

@kkirchner, I knew abou

@kkirchner,

I knew abou the easy way, but I was just wondering if it could be done the other way round :P Thanks for your time!

kkirchner's picture

@Vier, Both Tags and

@Vier,

Both Tags and Reviews are Magento core modules which you can disable in the admin. Once you disable them, the links will no longer show up on your customer account list of links.

Under System > Configuration > Advanced (bottom of left sidebar) > Disable Module Output. Select Disabled for Mage_Review and Mage_Tag.

Add comment

The content of this field is kept private and will not be shown publicly. If you have a Gravatar account associated with the e-mail address you provide, it will be used to display your avatar.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options