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&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.
English
简体中文
Deutsch
日本語
Español
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.
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.
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.
howdy,
is there a way to modify the link? i need to add a class to it (a different class to each link), eg.
Somethingmany thanks for an answer
osp
oh, sorry, please correct: a href=”something/” class=”class-of-something”
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.
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
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
For achieving the same with images, you would need to write some tiny conditional PHP code, which checks the current language and returns the image for that language.
Have a look here:
http://forum.wpml.org/topic.php?id=164#post-768
You could generalize it so that a single function is used for all images / CSS / whatever you need.
Thanks for your reply.
However the link to the image is to an image in a custom field, like this:
<img class="" src="” alt=”" />
No idea how to Include the ICL_LANGUAGE_CODE code into such link… i tried it in different ways, without luck. Is there a way to do it with a link like this one? I use the same image for all languages only the link should go to the page in the selected language.
Thank you,
Maggy
Can you start a thread about that in the forum? Would be easier to track it there.
Of course. you can find it here:
http://forum.wpml.org/topic.php?id=421&replies=1#post-2172
also with the right link, saw I missed the “ above.
Maggy
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?
Add your textdomain to the Gettext call. You’re probably getting translation from WordPress .mo file.
sorry i’m being stupid. The previous developer had set the css text-transform property to lowercase. doh!
It would be great if this function contained one more argument that could hold valueso of link attributes, such as title,class etc..
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
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.