Skip Navigation

This is the technical support forum for WPML - the multilingual WordPress plugin.

Everyone can read, but only WPML clients can post here. WPML team is replying on the forum 6 days per week, 22 hours per day.

Tagged: 

This topic contains 3 replies, has 2 voices.

Last updated by gerardoB-7 2 years, 6 months ago.

Assisted by: Andreas.

Author Posts
November 8, 2022 at 2:41 pm #12411577

gerardoB-7

I am a developer rolling a custom theme based on Bootscore, and I am wondering if there's any function to get the link to a certain page in the current language.

Example: I have a page "shipments" in the base language (english) and I have a translated slug as "envios" for spanish language.

In standard WP I'd use home_url("shipments") to get the page url but wp_home_url("shipments") will only yield the home page in the language, not the shipments page as expected ("/envios").

November 8, 2022 at 4:33 pm #12412595

Andreas

Hello there

Thank you for contacting us. I am happy to help you.

Have you checked our developer's guide?

https://wpml.org/documentation/support/

Inside those guides you will find also our hooks page :

https://wpml.org/wpml-hook/

Probably you are looking for this hook :

https://wpml.org/wpml-hook/wpml_element_link/

I hope this helps. Please let us know how it goes or if you need any further assistance. I'll gladly help you. 🙂

Regards,
Andreas

November 8, 2022 at 5:13 pm #12412923

gerardoB-7

Hi Andreas! Thanks both links are really useful. wpml_element_link() might be the closest to what i'm looking for. I've wrapped in function (see below) to call it by slug which is more preferable. I'd prefer to have a wpml_element_url() function that only brings the url without the to provide styling etc, but this is close enough.

In the end for this case, I'll be using a secondary menu location for the footer menu (this is to show some specific pages there) which translates automatically and I can set the css style as desired.

/**
 * Get current language url from original slug
 */
function wpml_lang_url($slug) {

   $page = get_page_by_path($slug);
   if ($page) {      
      $id = $page->ID;
      return apply_filters( 'wpml_element_link', $id, 'post', '', [], '', false );
   }
   
   return null;
}