【问题标题】:PHP "warning include_once(): Failed to open stream" in Wordpress themeWordpress 主题中的 PHP“警告 include_once():无法打开流”
【发布时间】:2020-04-11 12:28:36
【问题描述】:

我正在构建一个 WordPress 主题,并将用于自定义主题的函数分解到主题根目录中的 library 子目录中,并将它们包含在 include_once() 函数中。

我已将文件进一步分解为 library 内的子目录,并将每个文件都包含在正确的路径中。

我以这种方式包含近 20 个文件,没有任何问题,但我有一个文件,locations-post-type.php,位于 library/post-types 目录中,包含时会触发以下错误:

警告:include_once(library/post-types/locations-post-type.php):无法打开流:在 MY-THEME-PATH/wp-content/themes/caris/functions.php 中没有这样的文件或目录在第 98 行

警告:include_once(): 未能打开 'library/post-types/locations-post-type.php' 以包含 (include_path='.:/opt/alt/php70/usr/share/pear') 在 MY -THEME-PATH/wp-content/themes/caris/functions.php 第 98 行

/library/post-types 子目录中有 8 个文件,它们都具有相同的权限和所有权。运行ls -l 命令返回:

-rw-rw-r-- 1 rivalmi1 rivalmi1 5930 Jun 10  2019 careers-post-type.php
-rw-rw-r-- 1 rivalmi1 rivalmi1 5883 Jun 10  2019 events-post-type.php
-rw-rw-r-- 1 rivalmi1 rivalmi1 3066 Jun 10  2019 faqs-post-type.php
-rw-rw-r-- 1 rivalmi1 rivalmi1 3115 Jun 10  2019 history-post-type.php
-rw-rw-r-- 1 rivalmi1 rivalmi1 7181 Jun 10  2019 locations-post-type.php
-rw-rw-r-- 1 rivalmi1 rivalmi1 5121 Jun 10  2019 team-post-type.php
-rw-rw-r-- 1 rivalmi1 rivalmi1 3164 Jun 10  2019 testimonials-post-type.php
-rw-rw-r-- 1 rivalmi1 rivalmi1 6031 Jun 10  2019 workshops-post-type.php

我没有看到 locations-post-type.php 文件与我可以include_once() 没有错误的所有其他文件有任何区别。

另外,locations-post-type.php 在我的 XAMPP localhost 服务器上也可以正常工作,并且只会在我的登台服务器上导致错误。

有谁知道是什么导致了这个问题?

【问题讨论】:

    标签: php wordpress apache wordpress-theming


    【解决方案1】:

    尝试使用get_template_directory()(或get_stylesheet_directory() 用于子主题)以确保您需要来自您想要的位置的文件。

    require_once( trailingslashit( get_template_directory() ) . 'library/post-types/locations-post-type.php' );
    

    【讨论】:

      猜你喜欢
      • 2017-12-29
      • 2012-07-07
      • 1970-01-01
      • 2011-07-16
      • 2019-09-25
      • 2023-03-04
      • 1970-01-01
      • 2017-09-14
      相关资源
      最近更新 更多