duanliu8998 2015-02-11 11:06
浏览 90
已采纳

调用子目录中的页面模板

I have a static website in html which is multilanguage. So, I have created several subdirectories depending on the language is selected, this is, "lang" folder for the languages and "fr" folder for francais and "es" for spanish. However, I don't know how to call my template page in WordPress from the control panel for those pages are in another folder.

I have evaluated the issue and I know that is an option available from 3.4 version and you have to create a page-templates folder in the root of your theme, but in my case I'm able only to see my template pages which are in the root of my theme, not from the subdirectory.

Now my directory system is:

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

Any help would be much appreciated.

  • 写回答

2条回答 默认 最新

  • dongwupu5991 2015-02-11 17:36
    关注

    OK. It seems to me that you have a couple of options.

    1. Don't use specific page-templates for different languages.

    A better way of doing this would be to use the translating options that wordpress already provide.

    To learn more about them you should read at least:

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

    2. Translate your own way by using arrays or something

    You can easily create your own language file in php and write language translations to an array to translate your site.

    Example:

    File 1 - Language-EN.php

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

    File 2 - Language-NL.php

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

    File 3 - Page-Template.php

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

    or (if you are sure the $language variable is set correctly)

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

    3. As you asked: Load template from subdirectory

    Example:

    Create a page template in your themes root directory

    File : page-custom.php

    <?php
    /*
    Template Name: My Custom Page
    */
    
    include get_template_directory() . "/subdirectory/subpage-custom.php";
    

    Then create a new file in your subdirectory

    File: theme-root/subdirectory/subpage-custom.php

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

    Now you can select your page template from the post editor (under attributes)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?