droc60607 2016-09-27 12:27
浏览 80
已采纳

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。