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: Custom Work, Exception
This topic contains 11 replies, has 2 voices.
Last updated by Long Nguyen 1 year, 8 months ago.
Assisted by: Long Nguyen.
Author | Posts |
---|---|
November 17, 2023 at 9:23 am | |
corinneV-2 |
I am encountering a challenge with language switching on my WordPress site, specifically on pages of a custom post type named 'bien'. While the WPML plugin effectively translates the content and changes most URL parts accordingly, it fails to update the language prefixes (like /en, /ru) in the URLs when switching languages on these custom post type pages. |
November 17, 2023 at 10:02 am | |
November 20, 2023 at 2:58 am #14872505 | |
Long Nguyen WPML Supporter since 02/2022
Languages: English (English ) Timezone: Asia/Ho_Chi_Minh (GMT+07:00) |
Hi Corinne, Thank you for contacting WPML support, I'd be happy to help you with this issue. I see the language switcher displays the translation slug correctly, just missing the language code in the URL so it redirects to the 404 page. I suggest you add the language code when you translate the slug. For example: // Modification du lien pour les types de publication 'bien' function custom_post_type_link($link, $post) { if ($post->post_type === 'bien') { $station_terms = wp_get_post_terms($post->ID, 'station-de-ski'); $programme_terms = wp_get_post_terms($post->ID, 'programmes'); // Récupérer le code de langue actuel $current_language = apply_filters('wpml_current_language', NULL); $url = home_url('/'); // Déterminer les slugs en fonction de la langue actuelle switch ($current_language) { case 'en': // Anglais $url .= 'en/'; // add the language code to $url $station_slug = 'ski-resort'; $programme_slug = 'new-programs'; $bien_slug = 'property'; break; case 'ru': // Russe $url .= 'ru/'; // add the language code to $url $station_slug = 'лыжный-курорт'; $programme_slug = 'новые-программы'; $bien_slug = 'свойство'; break; default: // Langue par défaut (Français) $station_slug = 'station-de-ski'; $programme_slug = 'programme-neuf'; $bien_slug = 'bien'; break; } // Construire l'URL selon les termes disponibles et la langue if ($station_terms && !is_wp_error($station_terms)) { $url .= $station_slug . '/' . $station_terms[0]->slug . '/'; } if ($programme_terms && !is_wp_error($programme_terms)) { $url .= $programme_slug . '/' . $programme_terms[0]->slug . '/'; } $url .= $bien_slug . '/' . $post->post_name . '/'; return $url; } return $link; } add_filter('post_type_link', 'custom_post_type_link', 10, 2); If it does not help, please try to replicate the issue on a clean installation of WordPress. I will help you to check the issue. With this link, you can login directly to the admin area hidden link Kindly follow the steps below: - Don’t restore your site there. Look forward to your reply. |
November 20, 2023 at 7:57 am #14873553 | |
corinneV-2 |
Good morning, if you go to this url; the /en or /ru is added twice (example: en/en/ski-resort/courchevel/new-programs/la-calinette/property/3-rooms-courchevel-1650/) I don't need to have it twice |
November 20, 2023 at 8:53 am #14874033 | |
Long Nguyen WPML Supporter since 02/2022
Languages: English (English ) Timezone: Asia/Ho_Chi_Minh (GMT+07:00) |
Hi, Please try to replicate the issue on the sandbox site that I shared above, I will take a closer look. Thanks. |
November 20, 2023 at 9:28 am #14874371 | |
corinneV-2 |
i préfer to give you an access in my dashboard |
November 20, 2023 at 9:38 am #14874437 | |
corinneV-2 |
tell me where can i give you the access |
November 20, 2023 at 9:54 am #14874773 | |
corinneV-2 |
This is my actualy code in my functions.php /*REECRITURE DES URL POUR LES BIEN*/ // Boucle sur chaque langue et créer des règles de réécriture spécifiques // Ajouter des règles de réécriture pour chaque langue // Ajouter des paramètres de requête personnalisés // Modification du lien pour les types de publication 'bien' // Récupérer le code de langue actuel $url = home_url('/'); // Déterminer les slugs en fonction de la langue actuelle // Construire l'URL selon les termes disponibles et la langue if ($station_terms && !is_wp_error($station_terms)) { return $url; |
November 20, 2023 at 10:18 am #14874911 | |
Long Nguyen WPML Supporter since 02/2022
Languages: English (English ) Timezone: Asia/Ho_Chi_Minh (GMT+07:00) |
Hi, I would like to request temporary access (wp-admin and FTP) to your site to take a better look at the issue. It would be better to have a testing site where the issue is replicated. Your next reply is set to private to share the info. ❌ IMPORTANT: Please backup your database and website before proceeding ❌ Look forward to your reply. |
November 20, 2023 at 11:09 am | |
November 20, 2023 at 12:30 pm #14876727 | |
corinneV-2 |
Can you tel me if you need something ! |
November 21, 2023 at 2:33 am #14881525 | |
Long Nguyen WPML Supporter since 02/2022
Languages: English (English ) Timezone: Asia/Ho_Chi_Minh (GMT+07:00) |
Hi, The function home_url() returns the site URL including the current language code so I changed it to site_url() and now the language switcher works correctly. $url = site_url('/'); Here is the screen record hidden link Look forward to your reply. |
November 21, 2023 at 7:17 am #14882199 | |
corinneV-2 |
Oh, thank you very much. i'ts perfect. maybe you can help me for an other litle probleme. in my footer I have a js script which adds the country codes to the contact form. can you tell me why and how I fix this? New threads created by Long Nguyen and linked to this one are listed below: https://wpml.org/forums/topic/the-script-doesnt-work-on-the-translation-page/ |
November 22, 2023 at 1:43 am #14892395 | |
Long Nguyen WPML Supporter since 02/2022
Languages: English (English ) Timezone: Asia/Ho_Chi_Minh (GMT+07:00) |
Hi, We can handle one issue per ticket only so I will split your new issue with the JavaScript code on the translation page into a new ticket. If you see the main issue of this ticket with the custom post type slug is resolved, please mark it as Resolved. Thanks. |