WPML: getting page title in different language
-
16-10-2019 - |
Question
i'm running my web in english and german using the WPML plugin. my question: when in english mode - is it possible getting the page_title() but in german?
thanks
Solution
Let's say the original language of your site is english, then when visiting a german post you would return the title of the corresponding english post like that :
// Get the post ID of original post
$original_ID = icl_object_id( $post->ID, 'post', false, 'en' );
// Get original post title
$original_title = get_the_title( $original_ID );
Hope that helps, in any case check out the documentation for icl_object_id();
OTHER TIPS
Thanks for that mike23, was a lifesaver for me today. However that exact code didn't work for me, I did have to do some modifications in order to get it to work in my situation which may be due to a newer version of the WPML plugin.
I had to change:
$original_ID = icl_object_id( $post->ID, 'post', false, 'en' );
to:
$original_ID = icl_object_id( $post->ID, 'any', false, 'en' );
Notice that the only change is 'post' -> 'any'. It's a small change but made a world of difference as any other type that I entered would not work for me. Hope this helps someone that runs into the same issue.
PS: I found that the 'any' type could be used by going to the file where that function is created: /wp-content/plugins/sitepress-multilingual-cms/inc/template-functions.php. When in the file search for icl_object_id().