最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

include - get_template_part for template in subdirectory not working

programmeradmin19浏览0评论

I'm working on my first wordpress site, and moving it online. I have been adding some extra templates to design a simple booking engine, that was working perfectly on my offline localhost server.

However, I've been having trouble with getting these templates and other .php-files to work on the live website. It seems that using a specific path to locate the template or file doesn't work; get_template_part() and include() can only load the template/file when it is in the same folder.

For example, I have a page template called 'Bevestigingstemplate.php' in my Child-theme's folder (Theme-child), that I activated on one of my web pages. In this template, I want to call a file called 'bevestiging.php', that's located in Theme-child/modules/content.

However, if I call get_template_part('modules/content/bevestiging') OR include('modules/content/bevestiging'), it fails and I get a broken page. If however I move 'bevestiging.php' directly into my Child-theme's folder and call get_template_part('bevestiging'), it works like a charm.

Now I don't want to end up with a chaotic list of templates and sub-templates in my Child-theme folder, so I'm desperately looking for a way to fix this. I've read somewhere that I should maybe add a config.php file in my Child-theme's folder that defines a 'root', but I couldn't find a clear walkthrough and was hoping to maybe fix it in an easier way, or at least first to understand where I'm going wrong here.

Any advice on how to proceed is welcomed warmly. Thanks!

Below is the code I use in Bevestigingstemplate.php (basically just copied page.php from main theme).

get_header(); ?>

    <div id="primary" class="content-area <?php do_action('adviso_primary-width'); ?>">
        <main id="main" class="site-main">

            <?php
                get_template_part('modules/content/bevestiging');
                ?>

        </main><!-- #main -->

    </div><!-- #primary -->

<?php

get_footer();

I'm working on my first wordpress site, and moving it online. I have been adding some extra templates to design a simple booking engine, that was working perfectly on my offline localhost server.

However, I've been having trouble with getting these templates and other .php-files to work on the live website. It seems that using a specific path to locate the template or file doesn't work; get_template_part() and include() can only load the template/file when it is in the same folder.

For example, I have a page template called 'Bevestigingstemplate.php' in my Child-theme's folder (Theme-child), that I activated on one of my web pages. In this template, I want to call a file called 'bevestiging.php', that's located in Theme-child/modules/content.

However, if I call get_template_part('modules/content/bevestiging') OR include('modules/content/bevestiging'), it fails and I get a broken page. If however I move 'bevestiging.php' directly into my Child-theme's folder and call get_template_part('bevestiging'), it works like a charm.

Now I don't want to end up with a chaotic list of templates and sub-templates in my Child-theme folder, so I'm desperately looking for a way to fix this. I've read somewhere that I should maybe add a config.php file in my Child-theme's folder that defines a 'root', but I couldn't find a clear walkthrough and was hoping to maybe fix it in an easier way, or at least first to understand where I'm going wrong here.

Any advice on how to proceed is welcomed warmly. Thanks!

Below is the code I use in Bevestigingstemplate.php (basically just copied page.php from main theme).

get_header(); ?>

    <div id="primary" class="content-area <?php do_action('adviso_primary-width'); ?>">
        <main id="main" class="site-main">

            <?php
                get_template_part('modules/content/bevestiging');
                ?>

        </main><!-- #main -->

    </div><!-- #primary -->

<?php

get_footer();
Share Improve this question asked May 19, 2020 at 20:25 AmLamAmLam 111 silver badge3 bronze badges 2
  • Vancoder's answer below is correct - the reason for the adjustment is that you're using a child theme. If you were building an entirely custom theme, get_template_part() would work but since your templates are in a child theme directory you have to provide that extra information. – Tony Djukic Commented May 19, 2020 at 21:08
  • I just realised that the main problem is in 'bevestiging.php' itself. In it, I want to include a file called form.php that is not in the same directory as 'bevestiging.php', but in Theme-child/Includes. I tried `include ('/../../Includes/form.php'), but the page just breaks. – AmLam Commented May 19, 2020 at 21:31
Add a comment  | 

2 Answers 2

Reset to default 2

What you're looking for is get_stylesheet_directory().

get_template_part( get_stylesheet_directory() . '/modules/content/bevestiging.php');

This gives you the file path for the current theme.

In the end, the main problem was an 'include' in my bevestiging.php-file. I solved that by using include __DIR__ . '/../../Includes/form.php'. Thanks for the support!

发布评论

评论列表(0)

  1. 暂无评论