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 comes with an API function which does this:

icl_object_id(ID, type, return_original_if_missing,language_code)

  • ID – the ID of the post, page, tag or category
  • type – ‘post’, ‘page’, ‘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:

  1. Get the ID of the page in the language in which the gallery exists.
  2. Loop on the images for that page and display them.

5 Responses to “Language dependent IDs”

  1. BladHaund says:

    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?

  2. justin style says:

    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.

    • amir says:

      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.

  3. Madaíl says:

    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;

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="">