This thread is resolved. Here is a description of the problem and solution.

Problem:
Using a template page as page-{slug}.php, does not work in the secondary language.

Solution:
This is expected behavior. Since the page is selected by the slug, the different slug needs a different page.

You need to create pages template manually, one page for each slug or, if you prefer, you can create pages template with the comment at the top of the file. This way WPML can synchronize pages between languages and you will only need a one-page template. E.g.:

<?php
/**
 * Template Name: Example - About Us
 */
 
// content here

Relevant Documentation:
https://developer.wordpress.org/themes/template-files-section/page-template-files/

0% of people find this useful.

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 6 replies, has 4 voices.

Last updated by cindyL-2 3 years, 1 month ago.

Assigned support staff: Bruno.

Author Posts
April 26, 2017 at 9:23 am #1262027

norbertM-2

I am trying to:
I have Synchronize page template checked. I expect page-about.php to work for /about (English, default language) but also for /sobre-nosotros (Spanish) and /a-propos (French) which are translated versions of /about.

This is not the case, instead I have had to clone page-about.php into page-sobre-nosotros.php and page-a-propos.php. This is the case for several other pages that use custom templates.

URL of (my) website where problem appears:
hidden link, though you won't see this on the front end.
I expected to see:
as above
Instead, I got:
as above
Steps to duplicate the issue:
N/A

April 26, 2017 at 2:44 pm #1262417

Bruno
Supporter

Languages: English (English ) Portuguese (Brazil) (Português )

Timezone: America/Sao_Paulo (GMT-03:00)

Thank you for contacting us.

This should be synced correctly between languages. How are you setting up these pages templates?

Please as a test, could you disable all non-WPML plugins and see if the problem persists?

Also, please, what is the version of WPML and WP are you using? I would like to know more about your installation. Can you please provide your WPML Debug information in the debug field below? http://wpml.org/faq/provide-debug-information-faster-support/

Thank you.

April 26, 2017 at 2:58 pm #1262447

norbertM-2

Setting them up? Exactly as I said above.

These are NOT selectable page templates, eg where you give the template a name in a comment in the top of the template file.

page-about.php will be selected by WordPress for a page with slug 'about'. page-anythinghere.php the same will happen with a page with the slug 'anythinghere'.

This is core WordPress behaviour, using the the WordPress template hierarchy.

April 27, 2017 at 12:10 am #1262800

Bruno
Supporter

Languages: English (English ) Portuguese (Brazil) (Português )

Timezone: America/Sao_Paulo (GMT-03:00)

Thank you. I understood what you're talking about.

This seems to be normal behavior. Since the page is selected by the slug, the different slug needs a different page.

You need to create pages template manually, one page for each slug, as you are doing now or, if you prefer, you can create pages template with the comment at the top of the file. This way WPML can synchronize pages between languages and you will only need one page template. E.g.:

<?php
/**
 * Template Name: Example - About Us
 */

// content here

Please let me know if I can help with any other questions you may have.

Thank you.

May 15, 2017 at 10:11 am #1275925

norbertM-2

Hi Bruno sorry I didn't reply sooner - I had already changed to selectable page templates by the time you had replied as it seems liked the quickest fix. Lot's of people having this issue though - and not being able to use page templates like page-contact.php for example means WPML is preventing the use of a standard WP feature. Anyway thanks for the help.

August 23, 2018 at 3:14 pm #2670715

brianT-5

Changing the template in the WordPress admin is not ideal, nor is renaming and creating multiple page template file names in your theme, which becomes exponentially more challenging with additional languages.

Here's a helper function I wrote you can include in your theme functions file that automatically loads the default language template file regardless of the current language.
(Gist: hidden link)

/**
 * WPML helper function that returns original ids when translated versions don't exist
 * @since 1.0.0
 * @author ryanapsmith
 * @param integer content type id
 * @param string content type
 * @param string default language to show (optional)
 *
 * @return integer
 */

function _wpml_id( $id, $type, $lang = '' ) {
	if ( ! empty( $lang ) ) {
		$original = false;
	} else {
		$original = true;
	}

	return apply_filters( 'wpml_object_id', $id, $type, $original, $lang );
}

/**
 * WPML helper function to set the default language template regardless of current language
 *
 * @since 1.0.0
 * @author ryanapsmith
 * @return object
 *
 */

function _set_template_by_lang( $template ) {
	if ( function_exists( 'icl_get_languages' ) ) {
		global $sitepress;
		$default_lang = $sitepress->get_default_language();
		$lang         = ICL_LANGUAGE_CODE;

		if ( $lang != $default_lang ) {
			if ( is_page() ) {
				//get original page id
				$original_post_id = _wpml_id( get_the_ID(), 'page', $default_lang );
				//get slug of original page
				$slug = get_post_field( 'post_name', $original_post_id );

				$new_template = locate_template( array( 'page-' . $slug . '.php' ) );
				if ( ! empty( $new_template ) ) {
					return $new_template;
				}
			}
		}
	}

	return $template;
}

add_filter( 'template_include', '_set_template_by_lang', 99 );
September 5, 2018 at 7:12 pm #2708578

cindyL-2

Thank you so much @briant-5! The helper function you posted is fantastic.