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.

This topic contains 4 replies, has 2 voices.

Last updated by matM 1 year, 4 months ago.

Assigned support staff: Diego Pereira.

Author Posts
May 24, 2018 at 10:17 am #2221543

matM

Using an ACF True/False field I am trying to create an option to add a button which links to the translation of the page being displayed. Because this needs to go into the page template I cannot specify an ID or slug.

The code I have is as follows:

<?php if (get_field('include_translation_button') != "") { ?>
<div class="guidetrans-button">
<?php if(ICL_LANGUAGE_CODE=='en'): ?>
<p><a href="#">Guide Français</a></p>
<?php elseif(ICL_LANGUAGE_CODE=='fr'): ?>
<p><a href="#">English Guide</a></p>
<?php endif; ?>
</div>
<?php } else { ;?><?php } ;?>

What I need is an instruction to place in the <a href="#"> section that generates the URL for the page being displayed, in another language.

Can anyone help with this? It seems like something that should be quite straightforward but I can't find anything in the documentation/forums.

Thank you.

May 24, 2018 at 1:06 pm #2222940

Diego Pereira
Supporter

Languages: English (English ) Spanish (Español ) Portuguese (Brazil) (Português )

Timezone: America/Sao_Paulo (GMT-03:00)

Hello @matm, welcome to the WPML support forum!

The wpml_permalink hook can solve this. It filters a WordPress permalink and converts it to a language specific permalink based on the language url format set in the WPML language settings.

More details here: https://wpml.org/wpml-hook/wpml_permalink/

You code will look something like this:

<?php if(ICL_LANGUAGE_CODE=='en'): ?>
    <?php $wpml_permalink = apply_filters( 'wpml_permalink', '<em><u>hidden link</u></em>', 'fr' ); ?>
    <p><a href="<?php echo esc_url( $wpml_permalink  );?>">Guide Français</a></p>
<?php elseif(ICL_LANGUAGE_CODE=='fr'): ?>
    <?php $wpml_permalink = apply_filters( 'wpml_permalink', '<em><u>hidden link</u></em>', 'en' ); ?>
    <p><a href="<?php echo esc_url( $wpml_permalink  );?>">English Guide</a></p>
<?php endif; ?>

As an alternative, you can also use the language switcher on PHP code:

<?php do_action( 'wpml_language_switcher' ); ?>

More details: https://wpml.org/wpml-hook/wpml_language_switcher/

Please let me know if it worked for you.

All the best,
Diego

May 24, 2018 at 1:29 pm #2223200

matM

Thank you Diego,

Unfortunately, as I said in my original post, I can't have a hard coded link to a specific URL, ID or slug, because the template is used for several pages, and each needs to link to its translation.

<?php $wpml_permalink = apply_filters( 'wpml_permalink', 'hidden link', 'fr' ); ?> won't work in this case.

Perhaps the language switcher might work, but I am not clear on how to implement this. The site already offers a styled language switcher in the header and I don't want to repeat it in the template. This should simply be a text link to the same page in the other language.

Thanks.

May 24, 2018 at 1:41 pm #2223225

Diego Pereira
Supporter

Languages: English (English ) Spanish (Español ) Portuguese (Brazil) (Português )

Timezone: America/Sao_Paulo (GMT-03:00)

Hi @matm,

You can use get_permalink() to check the URL of the current post, and then add the filter.

It will look something like this:

  <?php $post_permalink = get_permalink(); ?>
  <?php $wpml_permalink = apply_filters( 'wpml_permalink', $post_permalink , 'fr' ); ?>
    <p><a href="<?php echo esc_url( $wpml_permalink  );?>">Guide Français</a></p>

Regarding the Language Switcher action, it's possible to wrap it on a div then style it independently:

<div class="my-custom-language-switcher">
    <?php do_action( 'wpml_language_switcher' ); ?>
</div>

It's also possible to build your own custom language switchers: https://wpml.org/documentation/getting-started-guide/language-setup/custom-language-switcher/

Please let me know if it worked for you.

Cheers,
Diego

May 24, 2018 at 3:34 pm #2224209

matM

Bingo - nice work Diego, thank you. It hadn't occured to me to combine that with get_permalink().