Wordpress Polylang多语言静态前端-page.php

Wordpress Polylang multi-language static front-page.php

正在寻找具有多语言网站和 Polylang 插件经验的人。

根据文档,可以使用静态首页: "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."

我已经完成了所有这些工作,为德文版创建了第二个模板 front-page-de.php,我在首页的德文版页面中使用了它。当我查看我的页面概览时,我看到原始版本和德语版本都被 Wordpress 标记为首页。然而,当我使用语言切换器切换到德语时,该网站继续使用原来的 front-page.php.

知道我做错了什么吗?


编辑


创建 nl.phpde.php 后的新 front-page.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');

?>

我想知道我是否必须执行 else 条件,但我不知道在未使用大括号 {} 时如何执行此操作。

以上代码来自:

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/ )

由于新模板不在任何特定的子文件夹中,因此我没有包含目录的 'template-parts' 部分。


编辑 2


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

 */

  $currentLanguage  = get_bloginfo('language');

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

        get_template_part('nl');
    }

    else {     
        get_template_part('de'); 
    }

?>

编辑 3


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

 */

  $currentLanguage  = pll_current_language();

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

        get_template_part('nl');
    }

    else {     
        get_template_part('de'); 
    }

?>

如您所述,文档指出:

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

因为它提到 "creating a page" 而不是 "creating a template",我假设你没有创建模板,你只需要:

  • 创建一个将由 front-page.php
  • 使用的页面
  • 在页面编辑器中,您可以通过单击语言框下的 + 号来添加另一种语言的页面
  • 您可能希望首页没有页面 URL,因此要避免这种情况,请转至 语言 > 设置 > URL 修改 并添加一个首页 url 的复选框包含语言代码而不是页面名称或页面 ID

更新

也许您可以使用代码 here that checks what is the current language and then use get_template_part 函数之类的东西来加载适当的模板。

更新 2

而不是 get_bloginfo('language'),尝试使用 polylang 函数 pll_current_language 发现 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.

您只需要创建一个文件,这就是您需要的模板。 编辑页面时,“语言”部分将出现在管理面板中。在本节中,您将能够编辑其他语言的页面。