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.

This topic contains 1 reply, has 0 voices.

Last updated by Dražen 1 week, 6 days ago.

Author Posts
June 13, 2025 at 11:36 am #17133139

consulta-europa-projectsM

Background of the issue:
I am trying to load a specific template for single posts with the 'news' category based on the language using WPML. I have implemented a function in WordPress to achieve this, which works for English, the default language, but not for Portuguese and Spanish. The function is not being called for these languages. Here is the code I am using: function load_custom_news_template_for_wpml( $template ) { if ( is_single() && has_term( 'news', 'category' ) ) { $language_code = defined( 'ICL_LANGUAGE_CODE' ) ? ICL_LANGUAGE_CODE : 'en'; error_log( 'Language code: ' . $language_code ); $new_template = locate_template( array( "single-news-{$language_code}.php" ) ); if ( ! empty( $new_template ) ) { error_log( "Loading language-specific template: " . $new_template ); return $new_template; } $new_template = locate_template( array( 'single-news.php' ) ); if ( ! empty( $new_template ) ) { error_log( "Loading fallback template: " . $new_template ); return $new_template; } } return $template; } add_filter( 'single_template', 'load_custom_news_template_for_wpml', 99 );

Symptoms:
The function to load language-specific templates is not being called for Portuguese and Spanish, only for English. The log file only shows entries for the English language code.

Questions:
Is there another way to control the template called in each case?

June 16, 2025 at 6:31 am #17137067

Dražen
Supporter

Languages: English (English )

Timezone: Europe/Zagreb (GMT+02:00)

Hello,

Thanks for contacting us.

While you wait for my colleague to take over the ticket, let me try to help you with the issue quickly.

It seem you are checking for post that have category news, which is not correct, translated post have translated category. Better would be to use ID of the category and get translated category ID for other languages using our hook wpml_object_id, for example:

$original_term_id = 123; // ID of 'news' in default language
$translated_term_id = apply_filters( 'wpml_object_id', $original_term_id, 'category', true );

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

Regards,
Drazen