调用子目录中的页面模板

Calling a page template which is in subdirectory

我有一个 html 的多语言静态网站。因此,我根据所选语言创建了几个子目录,即 "lang" 语言文件夹和法语 "fr" 文件夹以及西班牙语 "es" 文件夹。但是,我不知道如何从控制面板调用 WordPress 中的模板页面,因为这些页面位于另一个文件夹中。

我已经评估了这个问题,我知道这是一个从 3.4 版本开始可用的选项,您必须在主题的根目录中创建一个 page-templates 文件夹,但在我的情况下,我只能看到我的位于我的主题根目录中的模板页面,而不是来自子目录。

现在我的目录系统是:

wp-content -> themes -> myTheme -> [page-templates folder] - index.php / style.css -> [lang folder] -> [es folder] - about_me.php

如有任何帮助,我们将不胜感激。

WordPress 仅在主题目录和下面的一个 级别中搜索模板。为了做你想做的事,你需要适应这一点。例如:

/themes/myTheme
    /templates-en
    /templates-es
    ...

好的。在我看来,您有几个选择。

1。不要为不同的语言使用特定的页面模板。

更好的方法是使用 wordpress 已经提供的翻译选项。

要了解更多关于它们的信息,您至少应该阅读:

  1. Translating Wordpress
  2. The function _e()
  3. The function __()

2。用数组什么的翻译你自己的方式

您可以在 php 中轻松创建自己的语言文件,并将语言翻译写入数组以翻译您的网站。

示例:

文件 1 - 语言-EN.php

$language_array = array(
  "hello" => "hello",
  "world" => "world"
);

文件 2 - 语言-NL.php

$language_array = array(
  "hello" => "hallo",
  "world" => "wereld"
);

文件 3 - 页-Template.php

$language = "nl";
if($language = "nl")
{
    include 'language-nl.php';
}
elseif ($language = "en")
{
    include 'language-en.php';
}
echo $language["hello"];

或(如果您确定 $language 变量设置正确)

$language = "nl";
include "language-{$language}.php";
echo $language["hello"];

3。正如您所问:从子目录

加载模板

示例:

在主题根目录中创建页面模板

文件:页面-custom.php

<?php
/*
Template Name: My Custom Page
*/

include get_template_directory() . "/subdirectory/subpage-custom.php";

然后在您的子目录中创建一个新文件

文件:theme-root/subdirectory/subpage-custom.php

<?php echo "Your code goes here"; ?>

现在您可以从 post 编辑器(在属性下)select 您的页面模板