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();
  • 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 May 19 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 May 19 at 21:31
1

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.

| improve this answer | |
  • Thanks! This worked, but now I realised that the problem is actually in 'bevestiging.php' itself. See other comment. – AmLam May 19 at 21:30
-1

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!

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.