Is there a simple way to crate a link that works in both languages with Cornerstone and WPML

As I’m starting to work on more custom navigation and I have run into the problem that I don’t know how to link to stuff so that it works on both the main page and translated page. I also would like the link to survive if the slug of target were to change.

For example I am creating a mega-menu and I have a button that I want to link to an archive page for a custom post type, how can I create the link so that it works both when the header is on the Spanish and English version of any page?

I am assuming I could use twig to do something like the following, but that’s a lot of work and is hard to maintain, surely there is a better way.

{% set userlang = localization.user_locale %}
{% set lang = localization.locale %}
{% if userlang starts with 'es' %}/juegos/
	{% elseif userlang starts with 'en' %}/en/games
	{% elseif lang starts with 'es' %}/juegos
	{% elseif lang starts with 'en' %}/en/games
	{% else %}/jugeos
{% endif %}

do you have the WPML menu add-on installed? don’t take this the wrong way, but you might be doing it wrong. I’m pretty sure the menu add-on allows for the different language and will ultimately update everything menu related.

if that’s not working for you, I can share a custom add-on that I’ve developed. it automatically hits the same page in the other language when the language changes and keeps the menu in sync.

Hey Jeffrey,

Thank you for reaching out. I understand you would like a link in your header, for example a mega menu Button that points to a custom post type archive, to work on both the Spanish and English versions of every page and to keep working if the slug changes. There is a simpler way than a Twig condition. Thank you also to splaquet for the WPML menu suggestion, which covers links inside WordPress menus (see item 3). A Button’s link field is not part of a WordPress menu, so it needs the approach below. Please review the steps below.

  1. Use the Post Type URL dynamic content for archive links – In the Button’s link field, enter {{dc:post:post_type_url post_type="games" post="original"}}, replacing games with your custom post type’s key (the name it is registered with, not the translated URL slug). WordPress builds the archive URL from the post type each time the page loads, so nothing is typed in by hand, and the link follows any slug change. The post="original" part keeps the link working on pages that have no current post, such as a 404 page or an empty search result, where the link would otherwise be empty. If your homepage is set to show your latest posts rather than a static page, replace original with the ID of any published page.

  2. Check the link in both languages – WPML sets the site address for each language, and the archive URL is built from it. If you translate custom post type slugs under WPML > Settings > URLs and SEO (“Translate base slugs of custom post types and taxonomies”), please open a Spanish page and an English page and confirm the Button points to /juegos/ and /en/games/ respectively. If either version shows the wrong address, please let us know your WPML version and which language is your default.

  3. Menu links and links to specific pages – If a link sits in a WordPress menu shown with a Cornerstone Navigation element, translate that menu with WPML (WPML > WP Menus Sync), as splaquet suggested, and WPML will show the correct menu for each language. For a Button or other element that links to an individual page or post, you can use {{dc:post:permalink post="123"}} with the page’s ID. This also survives slug changes, but it always returns that exact page. In WPML, translate your header and use the ID of the Spanish page in the Spanish header and the ID of the English page in the English header.

Check out these documentations:

Regards.