Language dependent IDs
Many themes display special posts, pages and categories specially, usually based on IDs. For example, a ‘Featured’ posts section in the home page, or highlighted categories.
In some cases, these IDs are hard-coded into the theme’s PHP and in others, the user selects them in the theme’s setting screen.
To display correctly, the theme needs to select the IDs per language. In WPML, IDs for posts, pages, categories and tags change when the language changes, so the theme needs to use the correct IDs of the active language.
WPML includes two options for achieving this.
You can either enable the automatic ID adjustment or use the icl_object_id manually in your theme.
Which Option to Use?
The automatic ID adjustment requires no changes in your code. You can use it with any theme and everything “just works”. The down-side for using it is slightly increased processing time and DB access, as WPML will hook to many functions and check that the output is correct for the active language.
Manual ID adjustment using the icl_object_id function will achieve the same results and can be more efficient in terms of processing. However, it requires significant coding in the theme.
If you’re building a theme from scratch, you should consider using icl_object_id to do it manually. If you’re starting with an existing theme, hacking and modifying the theme might not be realistic and the automated ID adjustment is probably the way to go.
In any case, by using any caching plugin, the performance impact of this operation are reduced to none. Caching is always recommended for sites of any size. WPML runs fine with WP Super Cache and W3 Total Cache (which we also use on different sites).
1) Automatically Adjust IDs
WPML can hook to WordPress API functions, detect when specific items are loaded and adjust IDs so that the results are adjusted for the active language.
This functionality is enabled by default and can be accessed via WPML->Languages (visible in Advanced mode):

Auto-adjust IDs
This will automatically adjust all IDs and can be used in any WordPress theme that uses the API correctly (almost any theme you can find).
2) Manually, using the icl_object_id function
Alternatively, if the automatic ID adjustment is off, you can use the icl_object_id function to achieve the same manually.
icl_object_id(ID, type, return_original_if_missing,language_code)
- ID – the ID of the post, page, tag or category
- type – ‘post’, ‘page’, ‘post_tag’ or ‘category’
- return_original_if_missing – true if WPML should return the ID of the original language element if the translation is missing or false if WPML should return a NULL if translation is missing.
- language_code (optional) – if set, forces the language of the returned object and can be different than the displayed language.
The ID argument can be the ID of the object in any language. What the function will do is look up the translation group for that object and then the ID of the corresponding object in the active language.
Example usage
icl_object_id(3, 'category', false)
Return the ID of the category that is the translation of category 3. If it’s missing, return NULL.
How to use in themes
If you’re writing a theme that needs to be used with WPML, but you also want it to run correctly without WPML, you should wrap these calls by function_exists() calls. For example, you can create this function in your functions.php file:
function lang_category_id($id){
if(function_exists('icl_object_id')) {
return icl_object_id($id,'category',true);
} else {
return $id;
}
}
This function will return the ID of the translated category (if it exists) or the ID of the original category (if translation is missing). If WPML isn’t installed, the original category ID is always returned.
Translating arrays of IDs
Many times you need to convert an entire array of object IDs (most commonly category IDs). To do this, you can use the following function:
function lang_object_ids($ids_array, $type) {
if(function_exists('icl_object_id')) {
$res = array();
foreach ($ids_array as $id) {
$xlat = icl_object_id($id,$type,false);
if(!is_null($xlat)) $res[] = $xlat;
}
return $res;
} else {
return $ids_array;
}
}
This function also accepts the object type as an argument.
For example, to get an array of category IDs for categories 1,3 and 6 we’ll call:
lang_object_ids(array(1,3,6),'category')
Displaying page elements in different languages
The optional language_code argument allows mixing page elements in different languages.
For example, if you have an image gallery and don’t want to duplicate the images per language, follow this:
- Get the ID of the page in the language in which the gallery exists.
- Loop on the images for that page and display them.
I’m trying to implement a news feed in the sidebar that pulls content from a category within the same site, and produces output via the RSS sidebar widget. The pulled link needs to change according to the language. Any ideas?
Ok i have added the function, but how do i add the category to the featured slider, the category i want to reference is named ‘featured’ in my default language, and if i set it to that name in the theme options the slider works. but once i swap to secondary language it doesnt find the translated category.
This is a bit more complicated. If the theme supplied the category ID (as it should), you would just use the object_id function to get the ID of the translated category.
Since it supplies a name, you first need to find the ID (according to the name) and the look up the translated ID.
Hi there, awesome work with WPML.
I’ve read this through and as ive got no knowledge about php, could you enlight me how i should use the code in this specific one from the theme i use? this is the code:
function get_categname($cat_id){
global $wpdb;
$cat_name = $wpdb->get_var("SELECT name FROM $wpdb->terms WHERE term_id = '".$cat_id."'");
return $cat_name;
Using low level MySQL calls is much more difficult than using WPML’s API functions. Try using icl_object_id for that.
If you need technical help, better use the forum: http://forum.wpml.org
It seems to me that when specifying the second parameter as a tag, it should be ‘post_tag’, and not ‘tag’.
Thanks
For anyone looking for more info on this, WP 1.7.8 is compatible with custom taxonomies, so a query like this is valid:
icl_object_id(ID, custom_taxonomy_name, return_original_if_missing,language_code)
ex:
icl_object_id(56, ‘my_custom_taxonomy’, true);
I have a function that calls a post from the Features category
$my_query = new WP_Query(‘category_name=Features&showposts=1′); while ($my_query->have_posts()) : $my_query->the_post(); $do_not_duplicate = $post->ID;?>
How exactly do I implement this with wpml icl_object_id ?
thanks!
Could you please make some examples of code of the stuff written under displaying page elements in different languages.
I think it’s better for the people like me, not so skilled in PHP
I’m also getting the error: Invalid argument supplied for foreach with the function lang_object_ids! Is this function correct?
I second this. We need some more examples please of how this would work within a template. I have tried a few options now and not getting the results I need. I basically have a home page with a few different posts from different categories pulling in and I have followed the steps with no results.
Many thanks.
Hi, I use in a custom template this code at the top of the page:
get_header();if (!current_user_can('delete_others_posts')) {
wp_redirect(dentfix_access_denied());
}
then in functions.php I made this function
//send visitors to access denied pagefunction dentfix_access_denied(){
if (function_exists(icl_get_languages)) {
$id = icl_object_id(1614, 'page', true);
$cale = get_permalink( icl_object_id(1614, 'page', true) );
}
else
{ $cale = 'http://dentfix.ro/acces-restrictionat/';
$id=1614;
}
return $cale;
}
For some reason if I try to access a page from a translated language the redirect is performed to the page in default language.
Similar functions to point to other translated pages work fine, only this one is behaving strange.
This looks like a question for our technical forum.
If you need direct support from WPML’s developers, consider purchasing a support subscription.
Yes, sorry about that, I noticed later this is wrong place.
Anyway, the problem is that the page was never translated in the first place. My mistake…
Hi,
i’m running my web in english and german – my question:
when running in english mode – is it possible getting the title of the current page but in the original language (german)?
thanks
I’m not sure I understand the problem. Can you start a thread about it in our forum? Try to give an example of what you’re looking to achieve.
The icl_object_id function is very useful. I used it to reverse the direction of the translation to find the original object. I have a theme which loads different single template based on the category, so when I’m in spanish, it does not load the correct template. so i used icl_object_id and set the last item to ‘en’ which gave me my source category.
my only complaint is that in the code itself, the icl_object_id() function is not commented at all. And therefore I didn’t know it’s importance and utility until later after much trial and error. please comment the code a bit.
Yes please!
Hi, sorry if this is a stupid questions.
I have a fair few static elements in my page templates, e.g. a newsletter signup. I would like to display a different newsletter signup form depending on the language. Could u please give me advice on how to code it. Something like this:
IF ($language == de) {
FORM GERMANY
} ELSE if ($language == es)
FORM SPANISH
} ELSE {
FORM ENGLISH
}
Hello,
I run a multilingual website with WPML and english is the default language.
Is it possible to make appear the english version of a post when it is not translated in french or in german for example ?
Please help me.
You can use WPML’s content duplication for that:
http://wpml.org/2011/12/wpml-2-4-2-content-duplication-and-wysiwyg-custom-fields/