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?
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 );