2

Looking for someone with experience with multilingual sites and perhaps the Polylang plugin.

According to the documentation it is possible to use a static front page: "You have to create one page per language. Set translations as usual and then go to the WordPress ‘Reading settings’ panel. Check ‘Front page displays a static page’ and choose one of the page you have just created (the language doesn’t matter). Do the same for posts page if you use posts."

I've done all this, created a 2nd template front-page-de.php for the german version, and I'm using this in the german version of the Page used for the front page. When I look at my Pages overview, I see both the original version and the german version are tagged by Wordpress as Front Page. However when I use the language switcher to switch to german, the site continues using the original front-page.php.

Any idea what I'm doing wrong here?


EDIT


New front-page.php code after creating nl.php and de.php:

<?php
/**
 * Template Name: Front Page Template

 */

  $currentLanguage  = get_bloginfo('language');

  // Replace this condition with language code, ex : en-US
  if ( $currentLanguage == "nl-NL" ) 

        get_template_part('nl');

    $currentLanguage  = get_bloginfo('language');

// Replace this condition with language code, ex : en-US
if ( $currentLanguage == "de_DE" ) 

    get_template_part('de');

?>

I wondered if I had to do an else condition, but I've no idea how to do that when curly brackets {} are not being used.

I got the above code from:

Get a specific file

Although this kind of defeats the purpose of this function, it’s also possible to load a specific template file with it. All you have to do is use just one argument:

<?php get_template_part('template-parts/layout'); ?>

will include layout.php from template-parts subdirectory placed in the root of your theme folder. ( https://developer.wordpress.org/reference/functions/get_template_part/ )

As the new templates are not in any specific sub-folder I didn't include the 'template-parts' part of the directory.


EDIT 2


<?php
/**
 * Template Name: Front Page Template

 */

  $currentLanguage  = get_bloginfo('language');

    if ( $currentLanguage == "nl-NL" ) {

        get_template_part('nl');
    }

    else {     
        get_template_part('de'); 
    }

?>

EDIT 3


<?php
/**
 * Template Name: Front Page Template

 */

  $currentLanguage  = pll_current_language();

    if ( $currentLanguage == "nl-NL" ) {

        get_template_part('nl');
    }

    else {     
        get_template_part('de'); 
    }

?>
Erin
  • 85
  • 2
  • 13

2 Answers2

1

As you mentioned, the documentation states:

You have to create one page per language. Set translations as usual and then go to the WordPress...

Since it mentions "creating a page" and not "creating a template", I assume you don't create a template, you just need to:

  • create a page that would be used by front-page.php
  • when in page editor, you add a page for another language by clicking on + sign under Languages Box
  • you probably want front page to have no Page URL, so to avoid it, go to Languages > Settings > URL modifications and add a checkbox to The front page url contains the language code instead of the page name or page id

UPDATE

Maybe you can use something like the code here that checks what is the current language and then use get_template_part function to load the appropriate template.

UPDATE 2

Instead of get_bloginfo('language'), try to use the polylang function pll_current_language found here:

pll_current_language($value);

‘$value’ => (optional) either ‘name’ or ‘locale’ or ‘slug’, defaults to ‘slug’

returns either the full name, or the WordPress locale (just as the WordPress core function ‘get_locale’ or the slug ( 2-letters code) of the current language.

user8230352
  • 1,775
  • 1
  • 9
  • 13
  • This makes sense, thanks. However, my template contains some content which means I'd need two different templates depending on the language... or maybe some PHP that would display certain content depending on the language it's currently set to. Any idea how I could go about this? unfortunately my PHP knowledge is basic. Many thanks! – Erin Jul 18 '17 at 08:22
  • This looks exactly like the thing I'm looking for! However, I'm sure I'm doing something wrong still in the code. I've made 2 extra templates, `nl.php` and `de.php`. I will now update my original question with the code that's now in `front-page.php`. Any suggestions? – Erin Jul 18 '17 at 10:37
  • Try to use curly brackets in if statements. – user8230352 Jul 18 '17 at 10:55
  • Thanks for the reply, I tried incorporating curly brackets and the good news is that the PHP is readable, however it only shows the german template regardless of what language is picked. The german template is `de.php` and comes after the `else` statement (see edit above). Any idea what's wrong? – Erin Jul 18 '17 at 11:11
  • Maybe you need to use internal polylang function to check current language. Updated the answer. – user8230352 Jul 18 '17 at 11:26
  • Good point, thanks for the tip. I tried changing it (see edit above) but it still reverts to the german template all the time. – Erin Jul 18 '17 at 12:42
  • Try to echo the value returned by pll_current_language() for each language, so you can at least see if that part of the code works as it should be. Does the function return different values for each language and what are those values? – user8230352 Jul 18 '17 at 12:45
  • I tried to echo the words DUTCH and GERMAN for the respective languages - it just came up with GERMAN every time. Is this what you meant? – Erin Jul 18 '17 at 12:53
  • Try "echo $currentLanguage" to see what value does the variable have for each language. – user8230352 Jul 18 '17 at 12:55
  • With "echo $currentLanguage" it does indeed echo the appropriate 'nl' or 'de' for the relevant language. – Erin Jul 18 '17 at 13:10
  • Good. That means you just need to change "if ( $currentLanguage == "nl-NL" )" to "if ( $currentLanguage == "nl" )" – user8230352 Jul 18 '17 at 13:51
0

You just need to create one file, which will be the template you need. The "Languages" section will appear in the admin panel when editing the page. In this section you will be able to edit pages for other languages.