droc60607
2016-09-27 12:27
浏览 76

WordPress自定义页面模板从子目录中读取

I need guides please, as I am new to this.

So I installed WordPress locally and managed to create my custom theme with just the basic theme content.

Theme Directory: wp-content/themes/custom-theme/

In this directory I wish to create sub-directories (content/home, content/blog ...), and then I want my pages to read from this sub-directories, ect.

The homepage will read from custom-theme\content\home with the home directory folder including files like page.php.

I did already manage to create a template file called page-home.php which I linked as a template Parent page to the new page I created in wordpress. My problem is the files in that directory do not get hooked.

Code from page-home.php

     <?php 
          /*Template Name: Home - Parent Page */
     ?>

     <?php get_header(); ?>
         <div id="content-main">
     <?php $contentSrc = "content/home"; ?>

    <script>
        window.contentSrc = '<?php echo $contentSrc; ?>';
    </script>

    <?php //get_template_part("page-home"); ?>
    </div>
    <?php get_footer(); ?>

This is something I've tried. And it then should hook the pages from sub directory content/home .

Code from: content/home/page.php

   <div id="home">
     <div class="hook" data-hook="home-info"></div>
     <div class="hook" data-hook="home-navi"></div>
   </div>

This needs a function/js to work, but its just the idea of what I need and what I want to make happen.

Any links to guidelines for this or tips would be much appreciated, I have looked everywhere and i just do not find any easy or exact topic based on my question.

Maybe there is just a very easy way of doing it, i am interested.

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongtanghuan1885 2016-09-27 13:21
    已采纳

    One of the tools that WordPress offers is get_template_part();

    However, this requires you do use Wordpress' loop on the page calling this function.

    From the docs:

    Includes the named template part for a theme or if a name is specified then a specialized part will be included. If the theme contains no {slug}.php file then no template will be included.

    The template is included using require, not require_once, so you may include the same template part multiple times.

    Using with theme subfolders

    To use this function with subfolders in your theme directory, simply prepend the folder name before the slug. For example, if you have a folder called “partials” in your theme directory and a template part called “content-page.php” in that sub-folder, you would use get_template_part() like this:

    <?php get_template_part( 'partials/content', 'page' ); ?>
    

    Using PHP

    You can also use a simple include()/require()/etc. With the following code:

    <?php include( get_template_directory() . '/content/myfile.php'); ?>
    

    This particular example uses the get_template_directory() function to find your custom theme path. An alternative (but still useful) function to use is get_stylesheet_directory()

    Additional Resources

    已采纳该答案
    打赏 评论

相关推荐 更多相似问题