Title: Error including template part
Last modified: May 3, 2025

---

# Error including template part

 *  [timothy12](https://wordpress.org/support/users/timothy12/)
 * (@timothy12)
 * [1 year, 1 month ago](https://wordpress.org/support/topic/error-including-template-part/)
 * Trying to include a template part into my footer and index of this website, an
   error occured. All the content under the place where I located the script to 
   call the template part, went missing. The error message said that a critical 
   error has occured at this site and referred me to [this page](https://wordpress.org/documentation/article/faq-troubleshooting/).
 * I’ve tried this code snippet
 *     ```wp-block-code
                           <?php get_template_part( 'template-parts/subject-slide', get_post_type() );
   
                       else :
   
                       endif;
                       ?>
       ```
   
 * and this one:
 * `<?php get_template_part( 'template-parts/', 'subject-slide' ); ?>`
 * I don’t know why it is happening. I’m using the Underscores template, but not
   for the first time and I’ve been including template parts in this template before
   and never had issues.
   So I do need some help. Kind regards,Timothy
    -  This topic was modified 1 year, 1 month ago by [timothy12](https://wordpress.org/support/users/timothy12/).
 * The page I need help with: _[[log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fwordpress.org%2Fsupport%2Ftopic%2Ferror-including-template-part%2F%3Foutput_format%3Dmd&locale=en_US)
   to see the link]_

Viewing 11 replies - 1 through 11 (of 11 total)

 *  Moderator [threadi](https://wordpress.org/support/users/threadi/)
 * (@threadi)
 * [1 year, 1 month ago](https://wordpress.org/support/topic/error-including-template-part/#post-18448078)
 * Unfortunately, your code snippet is not enough to tell you the reason for the
   error. The section as it looks now is at least not complete, as the beginning
   of the if condition is missing.
 * If you want to investigate this further, check the error log to see what PHP 
   has documented there. Based on the messages there you should be able to recognize
   what the problem is. Alternatively, you can also activate debugging in WordPress
   as described here: [https://developer.wordpress.org/advanced-administration/debug/debug-wordpress/](https://developer.wordpress.org/advanced-administration/debug/debug-wordpress/)
 * Of course, you are also welcome to show us your entire source code so that we
   can assess this for you if necessary.
 *  Thread Starter [timothy12](https://wordpress.org/support/users/timothy12/)
 * (@timothy12)
 * [1 year, 1 month ago](https://wordpress.org/support/topic/error-including-template-part/#post-18448090)
 * Thanks for your quick reply and help, [@threadi](https://wordpress.org/support/users/threadi/).
   I’ve no experience with the error log, so could you tell me whre to find it?
 * And what do you mean about the entire source code? Of the template I want to 
   include the template part?
   Regards,Timothy
 *  Moderator [threadi](https://wordpress.org/support/users/threadi/)
 * (@threadi)
 * [1 year, 1 month ago](https://wordpress.org/support/topic/error-including-template-part/#post-18448131)
 * Your hoster’s support can help you find the error log. If this is not possible,
   I have already mentioned an alternative above with the debug mode.
 * In order to be able to evaluate what is wrong with the code you have written,
   you would have to see the entire code of the file you are editing.
 *  Thread Starter [timothy12](https://wordpress.org/support/users/timothy12/)
 * (@timothy12)
 * [1 year, 1 month ago](https://wordpress.org/support/topic/error-including-template-part/#post-18448148)
 * Hi [@threadi](https://wordpress.org/support/users/threadi/) ,
 * I’ve just contacted the helpdesk of my website to inform where to find the error
   log. Hopefully they reply as fast as you do! I’ve looked for the wp-config for
   the debug option. I’ve found that, but wait for a reaction of my host before 
   I try that.
 * The template page I want to include, is empty at the moment, besides of a few
   words to see if it works. As soon as it is well included, I will write the real
   content.
 * The source code of the index.php (in which I want to include on of my template
   parts), is the following:
 *     ```wp-block-code
       <div id="post-update">
           <div class="insidebox">
   
               <h1>The sinking of the Devonshire</h1>
   
               <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute <strong>irure dolor </strong>in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
   
               <p>Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et <u>quasi architecto</u> beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt.</p>
   
               <h2>White knight</h2>
               <p>Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore <i>magnam aliquam quaerat</i> voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? </p>
   
               <h3>Fall of house in Venice</h3>
               <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis 2<sup>e</sup> nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla <sub>3</sub>pariatur. Excepteur sint <b>occaecat</b> cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
   
               <h4>Message from an old friend</h4>
               <p>Quis autem vel <mark>eum iure reprehenderit</mark> qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?</p>
   
           </div><!-- /.insidebox -->
       </div><!-- /#post-update -->
   
   
       <div id="about">
           <div class="insidebox">
               About
           </div><!-- /.insidebox -->
       </div><!-- /#about -->
   
   
       <div id="post-random">
           <div class="insidebox">
   
   
   
                        <div class="subjects">
                <div class="slides">
   
   
                      <!-- Aardrijkskunde -->
                   <div class="slide">
                       <a href="<?php echo get_home_url(); ?>/aardrijkskunde">
                           <div class="slide-subject-image-aardrijkskunde"></div><!-- /.slide-mage -->
                           <div class="slide-name">Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni.</div><!-- /.slide-title -->
   
                       </a>    
                    </div><!-- /.slide --> 
   
   
                   <!-- Drama -->
                   <div class="slide">
                       <a href="<?php echo get_home_url(); ?>/drama">
                           <div class="slide-subject-image-drama"></div><!-- /.slide-mage -->
                           <div class="slide-name">Lorem ipsum dolor sit amet, consectetur adipiscing elit.</div><!-- /.slide-title -->
   
                       </a>    
                    </div><!-- /.slide --> 
   
   
                   <!-- Energizers -->
                   <div class="slide">
                       <a href="<?php echo get_home_url(); ?>/energizers">
                           <div class="slide-subject-image-energizers"></div><!-- /.slide-mage -->
                           <div class="slide-name">Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius.</div><!-- /.slide-title -->
   
                       </a>    
                    </div><!-- /.slide -->                  
   
   
   
   
   
                </div><!-- /.slides -->
           </div><!-- /.subjects -->
   
   
   
   
   
   
   
           </div><!-- /.insidebox -->
       </div><!-- /#post-random -->
       ```
   
 * <?php
   get_footer();
 * The template part I want to include at the place that says ‘About’. Most of the
   content is still dummy text which I’ve used to try a few things.
   Thanks for your
   help so far.
 *  Thread Starter [timothy12](https://wordpress.org/support/users/timothy12/)
 * (@timothy12)
 * [1 year, 1 month ago](https://wordpress.org/support/topic/error-including-template-part/#post-18448532)
 * Hello Threadi,
   I got a reply of my host and he adviced, just like you, to activate
   the debug mode. So I did and this is the message I got.
 * **Notice**: Undefined variable: the_query in **/home/erikdbh157/domains/erikdbh157.157.
   axc.nl/public_html/wp-content/themes/schoolworks/template-parts/subjects.php**
   on line **97**
 * **Fatal error**: Uncaught Error: Call to a member function have_posts() on null
   in /home/erikdbh157/domains/erikdbh157.157.axc.nl/public_html/wp-content/themes/
   schoolworks/template-parts/subjects.php:97 Stack trace: #0 /home/erikdbh157/domains/
   erikdbh157.157.axc.nl/public_html/wp-includes/template.php(812): require() #1/
   home/erikdbh157/domains/erikdbh157.157.axc.nl/public_html/wp-includes/template.
   php(745): load_template(‘/home/erikdbh15…’, false, Array) #2 /home/erikdbh157/
   domains/erikdbh157.157.axc.nl/public_html/wp-includes/general-template.php(206):
   locate_template(Array, true, false, Array) #3 /home/erikdbh157/domains/erikdbh157.157.
   axc.nl/public_html/wp-content/themes/schoolworks/index.php(49): get_template_part(‘
   template-parts/…’, ‘page’) #4 /home/erikdbh157/domains/erikdbh157.157.axc.nl/
   public_html/wp-includes/template-loader.php(106): include(‘/home/erikdbh15…’)#
   5 /home/erikdbh157/domains/erikdbh157.157.axc.nl/public_html/wp-blog-header.php(
   19): require_once(‘/home/erikdbh15…’) #6 /home/erikdbh157/domains/e in **/home/
   erikdbh157/domains/erikdbh157.157.axc.nl/public_html/wp-content/themes/schoolworks/
   template-parts/subjects.php** on line **97**
 * When in include one of the existing template parts, it worked well, but when 
   I tried to include the subject.php, this message occured. In the error message
   you see a bold part. I;ve emptied the file and now it seems to work. But, the
   other information, do I have to take action on that too?
   Kind regards,Timothy
 *  Moderator [threadi](https://wordpress.org/support/users/threadi/)
 * (@threadi)
 * [1 year, 1 month ago](https://wordpress.org/support/topic/error-including-template-part/#post-18448656)
 * What does your `template-parts/subjects.php` file look like? The error is in 
   line 97.
 *  Thread Starter [timothy12](https://wordpress.org/support/users/timothy12/)
 * (@timothy12)
 * [1 year, 1 month ago](https://wordpress.org/support/topic/error-including-template-part/#post-18448710)
 *     ```wp-block-code
                                   <!-- query auto -->                             <?php if ( is_page('auto') ) :                             ?>                                                   <?php                                            // the query                                            $the_query = new WP_Query(array(                                                'category_name' => 'auto',                                                'post_status' => 'draft',                                                'posts_per_page' => 3,                                                'orderby' => 'name',                                                'order' => 'asc',                                            ));                                        ?>                            <!-- query bedrijfswagen -->                            <?php                                elseif ( is_page('bedrijfswagen') ) :                            ?>                                            <?php                                            // the query                                            $the_query = new WP_Query(array(                                                'category_name' => 'bedrijfswagen',                                                'post_status' => 'draft',                                                'posts_per_page' => 3,                                                'orderby' => 'name',                                                'order' => 'asc',                                            ));                                        ?>                            <!-- query light electric vehicle -->                            <?php                                elseif ( is_page('lev') ) :                            ?>                                            <?php                                            // the query                                            $the_query = new WP_Query(array(                                                'category_name' => 'lev',                                                'post_status' => 'draft',                                                'posts_per_page' => 3,                                                'orderby' => 'name',                                                'order' => 'asc',                                            ));                                        ?>                            <!-- query bedrijfswagen -->                            <?php                                elseif ( is_page('fiets') ) :                            ?>                                            <?php                                            // the query                                            $the_query = new WP_Query(array(                                                'category_name' => 'fiets',                                                'post_status' => 'draft',                                                'posts_per_page' => 3,                                                'orderby' => 'name',                                                'order' => 'asc',                                            ));                                        ?>                             <?php else :                            ?>                                   <!-- op andere pagina's wordt er niks weergegeven -->                             <?php endif; ?><div class="mobility-slider">     <div class="middle-slider">         <div class="slides">    <?php if ($the_query->have_posts()) : ?>        <?php while ($the_query->have_posts()) : $the_query->the_post(); ?>     <div class="text-wrapper">        <div class="slide-image" style="background-image: url('<?php the_post_thumbnail_url(); ?>');"></div><!-- /.slide-mage -->        <div class="slide-data">            <div class="slide-title"><?php the_title(); ?></div><!-- /.slide-title -->               <div class="slide-content"><?php the_content(); ?></div><!-- /.slide-content -->        </div><!-- /.slide-data -->     </div><!-- /.text-wrapper -->         <?php endwhile; ?>        <?php wp_reset_postdata(); ?>    <?php else : ?>        <p><?php __('No News'); ?></p>    <?php endif; ?>         </div><!-- /.slides -->    </div><!-- /.middle-slider -->   </div><!-- /.mobility-slider -->  
       ```
   
 * This used to be the content of the subjects.php. Line 97 was
 * `**<?php if ($the_query->have_posts()) : ?>**`
 * Now, the content has changed and it works fine. I copied the original file from
   another website I recently made. For some reason, I don’t have the error there.
 *  Moderator [threadi](https://wordpress.org/support/users/threadi/)
 * (@threadi)
 * [1 year, 1 month ago](https://wordpress.org/support/topic/error-including-template-part/#post-18448801)
 * It is also clear why the error occurs. In line 97 you access the variable `$the_query`.
   This is only set at all under the conditions above. If none of the conditions
   apply (i.e. in the section where an HTML comment says `p andere pagina's wordt
   er niks weergegeven` this variable does not exist at all – this results in the
   PHP error.
 * You have 2 possibilities to solve this:
    - You also define a WP_Query in the else-branch which fills the variable `$the_query`.
    - You check whether the variable exists before line 97, e.g. like this:
 *     ```wp-block-code
       <?php if ($the_query && $the_query->have_posts()) : ?>
       ```
   
 *  Thread Starter [timothy12](https://wordpress.org/support/users/timothy12/)
 * (@timothy12)
 * [1 year, 1 month ago](https://wordpress.org/support/topic/error-including-template-part/#post-18448982)
 * Hi [@threadi](https://wordpress.org/support/users/threadi/) ,
 * Thats why it does work in the other side and not in the site I’m working now.
   I thought that the code said, if the page is called ‘auto’, than apply this query,
   and so on. In all other cases (else) it shouldn’t display a thing. In the other
   site, it apparantly works, but the code is not completely correct, am I right?
 * Your first solution sounds to me as the best one, but I don’t know exactly how
   to do it.
   Big thanks for your help!
    -  This reply was modified 1 year, 1 month ago by [timothy12](https://wordpress.org/support/users/timothy12/).
 *  Moderator [threadi](https://wordpress.org/support/users/threadi/)
 * (@threadi)
 * [1 year, 1 month ago](https://wordpress.org/support/topic/error-including-template-part/#post-18449337)
 * If you want to go with the first suggestion, you just have to replace
 *     ```wp-block-code
       <!-- op andere pagina's wordt er niks weergegeven -->
       ```
   
 * with the WP_Query you defined, e.g. the one above:
 *     ```wp-block-code
       <?php                                            // the query                                            $the_query = new WP_Query(array(                                                'category_name' => 'fiets',                                                'post_status' => 'draft',                                                'posts_per_page' => 3,                                                'orderby' => 'name',                                                'order' => 'asc',                                            ));                                        ?>
       ```
   
 * You just have to check whether the content fits. Nobody here can judge that for
   you, you have to know that because you know the project and are developing the
   theme.
 * If you have further questions about programming, I would recommend this platform:
   [https://wordpress.stackexchange.com](https://wordpress.stackexchange.com) – 
   there are also more users there who can answer such detailed questions. The forum
   here is intended for users, not for developers.
 * Alternatively, you can also look for someone here who can support you: [https://jobs.wordpress.net/](https://jobs.wordpress.net/)
 *  Thread Starter [timothy12](https://wordpress.org/support/users/timothy12/)
 * (@timothy12)
 * [1 year, 1 month ago](https://wordpress.org/support/topic/error-including-template-part/#post-18449733)
 * Thanks for helping me out. The other forum(s) you’ve mentionned, I didn’t know
   that until now. Those can be a big help in the future too. Like you ‘ve been 
   last days.

Viewing 11 replies - 1 through 11 (of 11 total)

The topic ‘Error including template part’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 11 replies
 * 2 participants
 * Last reply from: [timothy12](https://wordpress.org/support/users/timothy12/)
 * Last activity: [1 year, 1 month ago](https://wordpress.org/support/topic/error-including-template-part/#post-18449733)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
