Hard coded theme links

Many WordPress themes include hard coded links. For example, footer links that point to support and contact pages may look like this:

<a href="/support/">Support</a> | <a href="/contact-us/">Contact us</a>

When the site is running multilngual these links must adapt for the display language. Both the link text (what the visitor sees) and the URL (where the link is pointing to) need to change when switching languages.

WPML icl_link_to_element() function will produce localized links to pages and should be used for this purpose.

Function description

icl_link_to_element(ID, type, text, arguments, anchor);

Argument Description Required / optional
ID The ID of the post, page, tag or category to link to. Required
type The type of page to link to. Can be ‘post’, ‘page’, ‘tag’ or ‘category’. Optional, default is ‘post”
text The link text. If not specified will produce the name of the element in the current language. Optional, defaults to the element’s name. To leave at default, enter false.
arguments Optional arguments for the link. When used, this should be a PHP array. Optional, defaults to no arguments
anchor Optional anchor for the link. Optional, defaults to no anchor

Example usage

Example Purpose Produced HTML
<?php icl_link_to_element(10); ?> Link to page 10 (support page) <a href=”/support/”>Support</a>
<?php icl_link_to_element(10,’post’,__(‘Get help’)); ?> Link to support page with an alternative link text <a href=”/support/”>Get help</a>
<?php icl_link_to_element(10,’post’,false, array(‘category’=>’products’,'priority’=>’high’); ?> Link to support page and add two arguments <a href=”/support/?category=products&amp;priority=high”>Support</a>
<?php icl_link_to_element(10,’post’,false,’priority’=>’high’,'faq’); ?>
<?php icl_link_to_element(3,’tag’); ?> Link to tag with ID 3 (our ‘News’ tag) <a href=”/tag/news/”>News</a>

How it works

You can specify the ID of a page / post / tag / category in any language. WPML will check if translation exists for the current language. If so, it will produce the link to it. If translation doesn’t exist, it will link to the page in the original language.

When you specify the link text manually, it’s a good idea to wrap it in the gettext call __(), so that the text displays in the correct language. If you don’t specify any link text (as in the first example), WPML will produce the name of that page.

18 Responses to “Hard coded theme links”

  1. Pawel says:

    Please update that type argument defaults to ‘post’, not ‘page’. Also for page lookups type should be set to ‘post’ not ‘page’ to give proper results.

    • Amir says:

      Actually, in WordPress ‘pages’ and ‘posts’ share the same database table, so when you lookup a ‘post’ or ‘page’ using this function you get exactly the same thing.

      We kept both parameters so that the documentation makes sense, but they give the same result.

      • Pawel says:

        In my environment (WP 2.7.1, WPML 1.0.3) I’m receiving different results for ‘post’ and ‘page’. For ‘post’ as type argument I’m getting desired results, for same function call (only ID and type arguments) with ‘page’ as a type I’m receiving link to page with different ID.

        in inc/template-functions.php
        $trid = $sitepress->get_element_trid($element_id, $element_type)
        returns proper ID for call with ‘post’ as element_type and nothing for call with ‘page’ as element_type

        Apologies for starting discussion here, I know I should move to forum.

  2. othersphere says:

    howdy,
    is there a way to modify the link? i need to add a class to it (a different class to each link), eg.

    Something

    many thanks for an answer
    osp

    • othersphere says:

      oh, sorry, please correct: a href=”something/” class=”class-of-something”

    • admin says:

      We didn’t add optional styling to this call. I think that you can just wrap that link in a SPAN with a class. Then, in your CSS just style the links in the span class and you’ll get the results you need.

  3. Marco says:

    Hello
    I need to help for CSS.
    Why doesn’t implement CSS for functions ICL_LINK_TO_ELEMENT e ICL_GET_HOME_LINK? I want add css for these two functions. I’m resolve partially with div id=”menu-wrap” and using tag and , but doesn’t work the dropdown menu with subcategory. How can i do?

    Thank you for answer

    Bye

  4. Maggy says:

    Hi Amir,

    I really like your plugin, works perfect. Thank you for making it available for the community!

    I have a question regarding the above hard coded links. Is it possible to link this function to an image instead of text? I can’t get it to work…

    Thanks in advance.

    Maggy

  5. Adrian says:

    Hi I’m trying to use this method:
    icl_link_to_element(16, ‘post’, __(‘Home’))
    The translation itself works fine, however it returns everything in lower case for some reason. Any ideas?

  6. ieviesis says:

    It would be great if this function contained one more argument that could hold valueso of link attributes, such as title,class etc..

  7. K says:

    Is there a function to get only the URI? I have a 3 huge boxes as links on the front pages and everything is contained in the “a” tags.

    I know I can use the text variable inside this function, but it looks messy, I want less functionality for more flexibility :)

    • K says:

      I got my solution: I use get_permalink() from WP, with your icl_object_id, get get exactly what I want: get_permalink(icl_object_id(2, 'page', true)), replace 2 with the page to link to.

Leave a Reply

Please leave here comments about this page only.
For technical support and feature suggestions, head to our forum. We are waiting there!

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> <pre lang="" line="" escaped="">