【问题标题】:Why does include work but not require when including file in Wordpress child theme?为什么在 Wordpress 子主题中包含文件时包含工作但不需要?
【发布时间】:2015-10-26 08:25:22
【问题描述】:

谁能告诉我为什么...

我正在使用子主题,我想添加一个新的 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

为什么实际包含会报错?

同时

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

有效吗?

【问题讨论】:

    标签: php wordpress require


    【解决方案1】:

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

    include “工作”。而是throws a warning that it has not found the file

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

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

    require_once('test.php');
    

    这假设 test.phpfunctions.php 存在于同一目录中。

    您也可以使用get_stylesheet_directory() 检索当前主题/子主题的样式表目录路径

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

    【讨论】:

    • 好的,谢谢你的解释! :-) 我真的不想包含 test.php(我想你已经明白了),我想包含 recaptchalib.php 但是 require_once( 'recaptchalib.php' ) 不起作用,它给了我致命的错误。 - BUt require_once('test.php') 完美运行! :-)
    • 我想通了,这是我之前测试过的当前 recaptcha 插件的问题。当我停用它时,它可以完美地按预期包含 recaptchalib.php。
    猜你喜欢
    • 1970-01-01
    • 2018-10-09
    • 1970-01-01
    • 2014-03-15
    • 1970-01-01
    • 2017-06-02
    • 1970-01-01
    • 2014-07-15
    • 1970-01-01
    相关资源
    最近更新 更多