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
This topic contains 0 replies, has 2 voices.
Last updated by Andreas W. 1 week ago.
Assisted by: Andreas W..
Author | Posts |
---|---|
November 12, 2024 at 9:17 pm #16394695 | |
Mary |
Background of the issue: // Function to get translated ACF fields using WPML // Get the translated post ID using WPML's icl_object_id function if (!$translated_post_id) { // Retrieve the ACF fields for the translated post return $fields; // Shortcode for Ville field // Get translated fields return esc_html($fields['ville']); // Shortcode for Publics field $fields = get_translated_acf_fields($atts['post_id'], $atts['language']); return esc_html(implode(', ', (array)$fields['publics'])); // Shortcode for Programmes field $fields = get_translated_acf_fields($atts['post_id'], $atts['language']); return esc_html(implode(', ', (array)$fields['programmes'])); I then added the shortcodes in the table: [ville_field post_id="{post_id}" language="en"], [publics_field post_id="{post_id}" language="en"], etc. But nothing appears in those columns. Ninja Table does allow shortcodes and I tested a simple shortcode to see if it would display and it did. Symptoms: Questions: |
November 18, 2024 at 10:17 pm #16416853 | |
Andreas W. Supporter Languages: English (English ) Spanish (Español ) German (Deutsch ) Timezone: America/Lima (GMT-05:00) |
Hello, Take note that this is a custom code request and such requests are usually not supported on this forum. WPML Suppport Policy: Could you please give this a try? // Function to get translated ACF fields using WPML function get_translated_acf_fields($post_id, $language_code = null) { // Default to current WPML language if no language code is provided if (!$language_code) { $language_code = apply_filters('wpml_current_language', null); } // Retrieve the translated post ID $translated_post_id = apply_filters('wpml_object_id', $post_id, get_post_type($post_id), false, $language_code); // Return null if no translation exists if (!$translated_post_id) { return null; } // Retrieve ACF fields for the translated post $fields = array( 'ville' => get_field('ville', $translated_post_id), 'publics' => get_field('publics', $translated_post_id), 'programmes' => get_field('programmes', $translated_post_id), ); return $fields; } // Shortcode for Ville field function display_ville_field($atts) { // Parse shortcode attributes $atts = shortcode_atts( array( 'post_id' => get_the_ID(), 'language' => apply_filters('wpml_current_language', null), ), $atts, 'ville_field' ); // Get translated fields $fields = get_translated_acf_fields($atts['post_id'], $atts['language']); // Return the field value or a default message if not found return esc_html($fields['ville'] ?? 'Ville field not found'); } add_shortcode('ville_field', 'display_ville_field'); // Shortcode for Publics field function display_publics_field($atts) { // Parse shortcode attributes $atts = shortcode_atts( array( 'post_id' => get_the_ID(), 'language' => apply_filters('wpml_current_language', null), ), $atts, 'publics_field' ); // Get translated fields $fields = get_translated_acf_fields($atts['post_id'], $atts['language']); // Return the field value or a default message if not found return esc_html(implode(', ', (array)($fields['publics'] ?? array()))); } add_shortcode('publics_field', 'display_publics_field'); // Shortcode for Programmes field function display_programmes_field($atts) { // Parse shortcode attributes $atts = shortcode_atts( array( 'post_id' => get_the_ID(), 'language' => apply_filters('wpml_current_language', null), ), $atts, 'programmes_field' ); // Get translated fields $fields = get_translated_acf_fields($atts['post_id'], $atts['language']); // Return the field value or a default message if not found return esc_html(implode(', ', (array)($fields['programmes'] ?? array()))); } add_shortcode('programmes_field', 'display_programmes_field'); Improvements Made: Dynamic Post Type Handling: Error Handling: Dynamic Shortcode Attributes: Debugging: Uses get_the_ID() and apply_filters('wpml_current_language', null) as fallbacks. Debugging Tips: Enable WP DEBUG in wp-config.php: define('WP_DEBUG', true); define('WP_DEBUG_LOG', true); Check the logs in wp-content/debug.log. Add error logs inside get_translated_acf_fields() to confirm correct behavior: error_log("Post ID: $post_id, Language: $language_code, Translated ID: $translated_post_id"); If you need further asistance with this issue, I suggest you consult one of our contractors: We hope for your understanding. Best regards |
November 21, 2024 at 11:58 am #16428794 | |
Mary |
It didn't work, but since this is not covered by your support, I will try to find another solution. |
November 21, 2024 at 2:36 pm #16429645 | |
Andreas W. Supporter Languages: English (English ) Spanish (Español ) German (Deutsch ) Timezone: America/Lima (GMT-05:00) |
I would suggest you test the code without a Ninja Table first. If each Ninja Table is displayed with a shortcode then each Ninja Table has its proper ID. You will need to set Ninja Table as "Translatable" inside WPML > Settings > Post Type Translation and then translate the tables. Each table will then needed to be placed into each content in each language using their respective shortcodes. |