我正在尝试包含一个位于WordPress主题文件根目录下的PHP文件。
例如:localhost/mywordpresssite/wp-content/themes/mytheme/myfile.php 我正在尝试从位于“部件>共享”下的文件footer.php中调用它。
例如:localhost/mywordpresssite/wp-content/themes/mytheme/parts/shared/footer.php 我可以确认,在浏览器中访问完整URL时,该文件确实显示出来,因此路径是正确的。
我已经尝试了多种不同的方式无法包含此文件:
1)首选方式-使用样式表目录URI
这应该能找到样式表的URI并找到文件,但我却得到了这个错误信息:
例如:localhost/mywordpresssite/wp-content/themes/mytheme/myfile.php 我正在尝试从位于“部件>共享”下的文件footer.php中调用它。
例如:localhost/mywordpresssite/wp-content/themes/mytheme/parts/shared/footer.php 我可以确认,在浏览器中访问完整URL时,该文件确实显示出来,因此路径是正确的。
我已经尝试了多种不同的方式无法包含此文件:
1)首选方式-使用样式表目录URI
<?php
$stylesheet_root = get_stylesheet_directory_uri();
include $stylesheet_root.'/myfile.php';
?>
这应该能找到样式表的URI并找到文件,但我却得到了这个错误信息:
我知道这是因为它不安全,而且默认情况下找不到合适的包装器
allow_url_include
被关闭了,那么最好的方法是什么呢?2)在URL中编码以向上爬两个目录 这似乎是一个相当明智的方法,因为文件将始终相对两个目录而言。但这似乎没有任何作用。它仍然在同一个目录中寻找文件:<?php include '../../myfile.php'; ?>
3) 只需在代码中写死这个愚蠢的东西。
然而,即使在此处放置的URL已经在浏览器中测试并成功加载了正确的文件,但它仍然无法将其加载到包含文件中...
<?php include 'localhost/mywordpresssite/wp-content/themes/mytheme/twitter-feed.php'; ?>
出现错误:
警告:include(localhost/mywordpresssite/wp-content/themes/mytheme/twitter-feed.php):无法打开流:在/Applications/MAMP/htdocs/mywordpresssite/wp-content/themes/mytheme/parts/shared/footer.php的第3行没有此类文件或目录
还有什么我可以做的吗?
include( 'twitter-feed.php' )
是什么意思?它会产生什么结果? - BenMinclude '/var/www/mywordpresssite...
。 - CᴴᵁᴮᴮʸNᴵᴺᴶᴬget_stylesheet_directory_uri
会获取一个URI,但你需要一个本地文件系统路径。而且也有一个函数可以实现这个功能 -get_stylesheet_directory
。 - CBroe