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):
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.
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.