admin管理员组

文章数量:1302374

I have since tried a blank and new php file and accessing it the same way - that worked. So I am now unclear why the existing file does not load at all. There are no errors. Here is the code start of the template part I am getting:

<?php
   require_once WP_PLUGIN_DIR.'/themename/includes/file1.php';
   require_once WP_PLUGIN_DIR.'/themename/includes/file2.php';
   require_once WP_PLUGIN_DIR.'/themename/includes/file3.php';
     
   if(isset($_GET["ict"])){
       $id_category[] = "'{$_GET["ict"]}'";
       $variable1= new file3();

I am trying to include a partial template and have read up on how to do so.

<div class="row">
    <div class="col-md-12">
        <?php get_template_part('/partials/category.php');
        ?>
    </div>
</div> 

The file I have included the above code in is in the theme main. The partial is in a folder in the theme main called partials. When I use /partials/category.php I get a blank with no PHP error in the area of the page where the information should be.

If I dont use the / on front of partials I get an error.

I have also tried removing the / on the front and at the same time removing the .php at the end. Same result where there is no error and no output

What am I missing about getting the template part to display>?

Here is the blank space where this should display

I have since tried a blank and new php file and accessing it the same way - that worked. So I am now unclear why the existing file does not load at all. There are no errors. Here is the code start of the template part I am getting:

<?php
   require_once WP_PLUGIN_DIR.'/themename/includes/file1.php';
   require_once WP_PLUGIN_DIR.'/themename/includes/file2.php';
   require_once WP_PLUGIN_DIR.'/themename/includes/file3.php';
     
   if(isset($_GET["ict"])){
       $id_category[] = "'{$_GET["ict"]}'";
       $variable1= new file3();

I am trying to include a partial template and have read up on how to do so.

<div class="row">
    <div class="col-md-12">
        <?php get_template_part('/partials/category.php');
        ?>
    </div>
</div> 

The file I have included the above code in is in the theme main. The partial is in a folder in the theme main called partials. When I use /partials/category.php I get a blank with no PHP error in the area of the page where the information should be.

If I dont use the / on front of partials I get an error.

I have also tried removing the / on the front and at the same time removing the .php at the end. Same result where there is no error and no output

What am I missing about getting the template part to display>?

Here is the blank space where this should display

Share Improve this question edited Mar 3, 2021 at 1:13 fuxia 107k38 gold badges255 silver badges459 bronze badges asked Mar 2, 2021 at 23:35 user15081222user15081222 1033 bronze badges 18
  • 1 What is the error you get? Why do you include the .php extension? – Tom J Nowell Commented Mar 2, 2021 at 23:43
  • no error. Just tried it a number of ways. Its blank. I have since tried a blank and new php file and accessing it the same way - that worked. So I am now unclear why the existing file does not – user15081222 Commented Mar 2, 2021 at 23:49
  • what do you mean by tried a blank? And when you said "I get a blank with no php error in the area of the page" can you be more specific? I can think of multiple things this could mean. e.g the page stops loading when it reaches that line, or the page runs as if the line isn't present, etc. Have you checked your PHP error log? Are you sure partials/category.php actually has output? – Tom J Nowell Commented Mar 2, 2021 at 23:56
  • Thanks yes I am 100% it has output as it is used elsewhere successfully. I literally mean a blank in the space where I would expect to see the content. Just nothing there. – user15081222 Commented Mar 2, 2021 at 23:58
  • if it's used elsewhere successfully then can't you copy paste that to this location? What does the inside of that file look like? I do not understand what you mean by a blank in the space, it does not make sense as a phrase, the grammar makes no sense, can you describe using different words? – Tom J Nowell Commented Mar 3, 2021 at 0:00
 |  Show 13 more comments

1 Answer 1

Reset to default 0

Your get_template_part call is successful, it's the file it loads that is broken

<?php get_template_part( 'partials/category' ); ?>

If you swap the contents of that file for this:

<p>Hello World</p>

Then it will work as expected.

The cause of your problem is this:

require_once WP_PLUGIN_DIR.'/themename/includes/file1.php';
  • you should not be including files from other themes directly
  • themes don't go in the plugins/WP_PLUGIN_DIR folder
  • PHP can't find the file so it exits with a fatal error

You should see confirmation of this in your PHP error log

本文标签: Template part including not working