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! -->
                    <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

30 Responses to “Editing Magento’s Top Links (The Better Way)”

  1. thanks! BTW, there’s a missing close bracket on

  2. Sadly, this code chokes on wishlist for me under 1.4.0.0. Any ideas?
    a:5:{i:0;s:278:”Warning: call_user_func_array() [function.call-user-func-array]: First argument is expected to be a valid callback, ‘Mage_Wishlist_Helper_Data::’ was given in \app\code\core\Mage\Core\Model\Layout.php on line 326″;i:1;s:1934:”#0 \app\code\core\Mage\Core\Model\Layout.php(326): mageCoreErrorHandler(2, ‘call_user_func_…’, ‘C:\web\root\…’, 326, Array)

  3. Thanks bluescrubbie,

    It looks like in 1.4 they changed the wishlist.xml – just replace:

    <action method="removeLinkByUrl"><url helper="wishlist/"/></action>

    with:

    <remove name="wishlist_link"/>

    Because of the “remove” global effect, if you want to re-add the wishlist link somewhere else, you need to create a custom block. I just copy it from wishlist.xml, change the name attribute of the block, and place it in my local.xml file.

    <reference name="WHEREVER">
        <block type="wishlist/links" name="wishlist_link_custom">
            <action method="addWishlistLink"></action>
        </block>
    </reference>
  4. Thank you, this is extremely helpfull

  5. Thanks for the info on removing links; I’m trying to add a link to a cms page and to the homepage using the method above, but the links generated are not parsed in any way eg. home just passes “home” as the complete anchor href element href=”home” which is incorrect.

    The same is true for my CMS page, which breaks these links from pages where the base url is wrong, like the customer account page; the links suddenly become href=”customer/account/home” and “customer/account/customer-service” for example.

    Any thoughts on how to make Magento parse the page names and insert fully qualified URI’s as it does for ‘helper’ links like the customer login & basket links?

  6. @Russ – thanks for that question.

    I dug into the app/code/core/Mage/Page/Block/Template/Links.php file. You can use the full url in your ‘url’ tag or add a param to the addLink method. The latter is shown here – in your local.xml:

        <action method="addLink" translate="label title">
            <label>About Us</label>
            <url>about</url>
            <title>About Us</title>
            <prepare>true</prepare> <!-- make this true -->
            <urlParams>http://example.com</urlParams> <!-- pass your base url here -->
            <position>1</position>
        </action>
  7. I am trying to do the same thing, however I cant seem to get it to work atall without crashing. I want to add 2 links to the top link of the site that direct to a CMS page. What files do I modify?

    thanks in advance :)

  8. Wow both this is awesome! I have been fighting magento for going on a year. hopefully with help from this we will be able to streamline our magento development

  9. Thanks for the great manual!
    My problem is with multilanguage shops.
    Can you say me how to translate, for example, About us to “&Uml;ber uns”.

  10. Thanks for this, very helpful!

  11. Hi

    Using 1.4, the customer logged on/out code doesn’t work.

    Ay ideas gratefully received.

    Thanks

    Jerry

  12. Thanks kkirchner,

    I didn’t want to hard-code links as we run about a dozen sites, But I did a little digging myself and came up with this code for local.xml which uses a url helper to generate the correct URL for the homepage of the current site.

    <action method="addLink" translate="label title">
    <label>Home Page</label>
    <url helper="core/url/getHomeUrl"/>
    <title>Home Page</title>
    <prepare/>
    <urlParams/>
    <position>1</position>
    </action>
  13. Wow, Russ! That looks great. I’m definitely going to check this out – that could be super helpful!

  14. @Jerry – I tested the logged in and logged out on 1.4 with the blank theme and it worked for me… double check and make sure they’re after the </default> tag.

  15. There is an error in the local.xml posted. The beforeText tag was not properly closed.

    Kindly change to:

  16. Also, kindly change the local.xml posted above to remove the log out url when the customer is logged out and to remove the log in url when the customer is logged in.

  17. @James Wyson – Can you wrap your code examples in pre tags? See the example code above the comment box for details.

  18. @James Wyson – Thank you for catching that error. I’ve properly closed the beforeText tag.

    Also, the log in link only shows when the user is logged out. And conversely, the log out link only shows when the user is logged in. So you have to wrap the remove login link in the customer_logged_out tag and the remove logout link in the customer_logged_in tag. The code shown above for local.xml is correct.

  19. Very much apprecaited – very helpful!

  20. Sorry for the amount of spam, the code and pre tags didn’t work for me :(

    Hi, I’m trying to edit the customer_account_navigation block to remove the My Tags and My Product Reviews links but I can’t make it. This is what I’ve tried so far in local.xml:

    <customer_account>
    <reference name="customer_account_navigation">
    <remove name="tags"/>
    </reference>
    </customer_account>

    Also,

    <action method="removeItem"><name>tags</name><path>tag/customer/</path><label>My Tags</label></action>
    <action method="unsetChild"><name>tags</name></action>
    <action method="removeLinkByUrl" module="tag"><url>http://www.localhost.com/magento/index.php/tag/customer/</url></action>
    <action method="removeLinkByPath" module="tag"><name>tags</name><path>tag/customer/</path></action>

    Do you have any ideas? Thanks for the help!

  21. @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.

  22. @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!

  23. 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.

  24. 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).

  25. @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.

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

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

  28. @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!

  29. 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

  30. 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

Comment Form

For Code: Use the html code for < & >.  i.e. "&lt;YOUR_TAG&gt; ...your code... &lt;/YOUR_TAG&gt;"

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Call us at 417-597-3397, email us at sales@classyllama.com, or use this form to contact us:

  1. (required)
  2. (required)
 

cforms contact form by delicious:days

Full builds (design and development) start at $70k