Search code examples
wordpressmultilingualpolylang

Wordpress Polylang multi-language static front-page.php


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'); 
    }

?>

Solution

  • 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.