为什么在 Wordpress 子主题中包含文件时包含工作但不需要?

Why does include work but not require when including file in Wordpress child theme?

谁能告诉我为什么...

我正在使用儿童主题,我想添加一个新的 php 文件 test.php。

我把这个放在functions.php

//Gives correct file and path to the child-theme
echo get_bloginfo('stylesheet_directory') . "/test.php"; 

//gives me a blank page (error).
require_once(  get_bloginfo('stylesheet_directory') . "/test.php" ); 
//require(  get_bloginfo('stylesheet_directory') . "/test.php" ); also throws an error

为什么实际include会报错?

同时

//works perfectly
include(  get_bloginfo('stylesheet_directory') . "/test.php" ); 

有效吗?

According to the Codexget_bloginfo('stylesheet_directory') returns 活动主题的 样式表目录 URL。这不是文件路径...它是 URL.

include 不会 "work"。相反,它 throws a warning that it has not found the file.

另一方面,

require 会引发致命错误(这会导致空白页,因为您没有错误报告)。

包含此文件的正确方法是使用如下内容:

require_once('test.php');

这假定 test.phpfunctions.php.

存在于同一目录中

您还可以使用 get_stylesheet_directory() 检索当前 theme/child 主题的样式表目录 path

require_once( get_stylesheet_directory() . '/test.php' );