Retrieving Other language Links using WPML Language Code and Post ID retrieved from Post URL

65 Views Asked by At

I am building a Custom Language Selector page that displays all the languages enabled in WPML in a separate Page for changing languages instead of the Language Selector widget.

I am getting the previous page URL via

$_SERVER['HTTP_REFERER']

I am getting Post/page ID using the below function.

url_to_postid();

Then I have a foreach loop which returns the Language Code. I want to get the correct URL based on the Language code and Post ID.

I tried using the below code for getting this done but its not working as expected.

$previous_post_link = apply_filters( 'wpml_permalink', get_the_permalink( $last_visited_id ), $language_code );

This works partly. When I use this function, I get the URL of the Post ID based on the language code but the slug is not correct. "es" is the language code Below the URL is the URL of the Post ID that I send.

https://www.domainname.com/kontakt/ 

The actual Spanish version of this URL and the expected result is below.

https://www.domainname.com/es/contacto/

Instead, this displays the URL wrongly as

https://www.domainname.com/es/kontakt/

Can someone help me understand the issue and help me solve this?

Thanks in advance.

0

There are 0 best solutions below