简体   繁体   中英

Stuck on include() in a wordpress theme

I'm going to include() a php file in a wordpress theme file. The file is header.php, and the function is like:

<?php
error_reporting(E_ALL);
$filename = "path/to/file.php";
if (file_exists($filename)){
echo 'Ok';
include($filename);
} ?>

"Ok" is printed in resulting html, but output stops immediately after. I used both a relative and an absolute path with the same result. Am I missing something about home themes work?

File permissions are ok.

EDIT: display_errors is set off in wordpress. I had to enable it to find the error and resolve. It was a FATAL.

$filename = "file.php"; // First of all that needs to be quoted

// then its $filename, not filename. Missing $ before variable name

if (file_exists($filename)){  
echo 'Ok';
include($filename);
}

okay 2 things here .. if you have a piece of code after include statement request you to change it to the following format ..

include('".$filename."');

And secondly even if its a blank file, it always best to have quotes in it.

Also could let paste any specific error logs for this issue ..

I would also recommend to use include_once() instead of include() if in function, otherwise the file will be included as many times as the function is called, possibly causing errors and definitely causing server resource waste.

Another advice is to use file name without path at all if both files caller and called are located in the same folder. In most cases this would be a better option (unless current working directory of the script differs from directory of the caller file) as when moving your files to another server your paths might change causing your scripts to stop working properly. That's when you're going to need to go file by file fixing up your paths.

如果您的文件在模板中,请按照以下说明添加文件

<?php include (TEMPLATEPATH . '/path/to/file.php'); ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM