Magento Top Links XML

At the top of a Magento website, there are top links. These top links are not defined within template files but through Magento’s layout configuration.

The RWD theme’s top links are found by clicking the Account link in the header of the website.

Magento Top Links XML

The top links are not all defined within one layout file, but several. The My Account and Register links are defined within the customer.xml file via the Mage_Customer module.

<?xml version="1.0"?>
<layout version="0.1.0">
    <default>
        <!-- Mage_Customer -->
        <reference name="top.links"&gt;
            <action method="addLink" translate="label title" module="customer"><label>My Account</label><url helper="customer/getAccountUrl"/><title>My Account</title>&<prepare/><urlParams/><position>10</position></action>
            <action method="addLink" translate="label title before_text" module="customer"><label>Register</label><url helper="customer/getRegisterUrl" /><title>Register</title><prepare/><urlParams/><position>100</position><li/><a/></action>
        </reference>
        ....
    </default>
    ....
</layout>

The Wishlist top link is defined within the wishlist.xml layout file via the Mage_Wishlist module.

<?xml version="1.0"?>
<layout version="0.1.0">
    <default>
        <reference name="top.links">
            <block type="wishlist/links" name="wishlist_link" />
            <action method="addLinkBlock"><blockName>wishlist_link</blockName></action>
        </reference>
        ...
    </default>
    ....
</layout>

The My Cart and Checkout links are defined within checkout.xml via the Mage_Checkout module.

<?xml version="1.0"?>
<layout version="0.1.0">
    <default&gt;
        <!-- Mage_Checkout -->
        <reference name="top.links">
            <block type="checkout/links" name="checkout_cart_link">
                <action method="addCartLink"></action>
                <action method="addCheckoutLink"></action>
            </block>
        </reference>
        ....
    </default>
    ....
</layout>

The Log In link is also defined within customer.xml, however the configuration is within a pair of <customer_logged_out> nodes. This means that the link will only show when the customer is logged out.

<?xml version="1.0"?>
<layout version="0.1.0">
    ....
    <customer_logged_out>
        <!---<reference name="right">
            <block type="customer/form_login" name="customer_form_mini_login" before="-" template="customer/form/mini.login.phtml"/>
        </reference>-->
        <reference name="top.links">
            <action method="addLink" translate="label title" module="customer"><label>Log In</label><url helper="customer/getLoginUrl"/><title>Log In</title><prepare/><urlParams/><position>100</position></action>
        </reference>
        <remove name="reorder"></remove>
    </customer_logged_out>
    ....
</layout>

Each of these top links can be removed via configuration within a custom theme’s local.xml file. Assuming your theme’s parent is the RWD theme, the following syntax can be added.

<?xml version="1.0"?>
<layout>
    <default>
        <reference name="top.links">
            <action method="removeLinkByUrl">
                <url helper="customer/getAccountUrl"/>
            </action>
            <remove name="wishlist_link"/>
            <!-- Removes both checkout and cart links -->
            <remove name="checkout_cart_link"/>
            <action method="removeLinkByUrl">
                <url helper="customer/getRegisterUrl"/>
            </action>
        </reference>
    </default>

    <customer_logged_out>
        <reference name="top.links">
            <action method="removeLinkByUrl">
                <url helper="customer/getLoginUrl"/>
            </action>
        </reference>
    </customer_logged_out>
</layout>

As the checkout and cart links are added together under one block, it doesn’t seem possible to remove the checkout and cart links separately. Therefore to remove one or the other, remove the block that contains both links and re-add the link(s) separately.

<?xml version="1.0"?>
<layout>
    <default>
        <reference name="top.links">
            <remove name="checkout_cart_link"/>
            <block type="checkout/links" name="checkout_cart_link_new">
                <action method="addCartLink"></action>
                <action method="addCheckoutLink"></action>
            </block>
        </reference>
    </default>
</layout>

If you want to add a custom link to the top links menu, you can use the addLink() method, and add code similar to the below example in your theme’s local.xml file.

<?xml version="1.0"?>
<layout>
    <default>
        <reference name="top.links">
            <action method="addLink" translate="label title">
                <label>About Us</label>
                <url>about</url>
                <title&gt;About Us&lt;/title>
                <prepare />
                <position>800</position>
            </action>
        </reference>
    </default>
</layout>

Unfortunately when adding custom top links, using the position attribute doesn’t always position the link in the order you might expect in the top links menu. Therefore, you might have to remove the top links completely, and re-add each link individually specifying the correct position.

Note: This article is based on Magento Community/Open Source version 1.9.