我正在尝试包含一个PHP文件,它位于我的Wordpress主题文件的根目录中.
例如localhost/mywordpresssite/wp-content/themes/mytheme/myfile.php
我试图从footer.php位于的文件中调用它parts > shared
例如localhost/mywordpresssite/wp-content/themes/mytheme/parts/shared/footer.php
我可以确认,当访问浏览器中的完整URL时,文件确实显示,所以它绝对是正确的路径.
我无法以多种不同的方式包含此文件:
<?php
$stylesheet_root = get_stylesheet_directory_uri();
include $stylesheet_root.'/myfile.php';
?>
Run Code Online (Sandbox Code Playgroud)
这应该找到样式表URI然后找到该文件,但是我收到此错误:
没有找到合适的包装纸
我理解这种情况发生的原因是它不安全并且allow_url_include默认情况下是关闭的,那么最好的方法是什么?
这似乎是一种相当明智的方式,因为文件将始终是彼此相对的两个目录.但这似乎什么都不做.它仍然在同一目录中查找文件:
<?php include '../../myfile.php'; ?>
Run Code Online (Sandbox Code Playgroud)
然而,即使此处放置的URL在浏览器中进行了测试并成功加载了正确的文件,此STILL也不起作用.它不会加载到包含的文件中...
<?php include 'localhost/mywordpresssite/wp-content/themes/mytheme/twitter-feed.php'; ?>
Run Code Online (Sandbox Code Playgroud)
给出错误:
警告:include(localhost/mywordpresssite/wp-content/themes/mytheme/twitter-feed.php):无法打开流:/ Applications/MAMP/htdocs/mywordpresssite/wp-content/themes/mytheme中没有此类文件或目录第3行/parts/shared/footer.php
我还可以做些什么?
你正试图include通过URL.你需要include通过文件的路径(而不是uri):
<?php
$stylesheet_root = get_stylesheet_directory();
include( $stylesheet_root . '/myfile.php' );
?>
Run Code Online (Sandbox Code Playgroud)
阅读文档中有关get_stylesheet_directory()的更多信息.