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.
I’ve tried this code snippet
<?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 ago by
timothy12.
The page I need help with: [log in to see the link]
-
This topic was modified 1 year ago by
-
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.ww.wp.xz.cn/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.
Thanks for your quick reply and help, @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,
TimothyYour 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.
Hi @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:
<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.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,
TimothyWhat does your
template-parts/subjects.phpfile look like? The error is in line 97.<!-- 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.
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 saysp andere pagina's wordt er niks weergegeventhis 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:
<?php if ($the_query && $the_query->have_posts()) : ?>Hi @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 ago by
timothy12.
If you want to go with the first suggestion, you just have to replace
<!-- op andere pagina's wordt er niks weergegeven -->with the WP_Query you defined, e.g. the one above:
<?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 – 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/
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.
- You also define a WP_Query in the else-branch which fills the variable
The topic ‘Error including template part’ is closed to new replies.