php - 无法包含 Wordpress 主题文件夹中的 PHP 文件

标签 php wordpress

我试图在我的 Wordpress 主题文件的根目录中包含一个 PHP 文件。

E.g localhost/mywordpresssite/wp-content/themes/mytheme/myfile.php

我正在尝试从位于 parts > shared 的文件 footer.php 中调用它

E.g 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,然后找到文件,但我得到这个错误:

no suitable wrapper could be found

我知道发生这种情况的原因是它不安全,而且 allow_url_include 默认处于关闭状态,那么最好的方法是什么?

2) 在URL中编码爬升两个目录

这似乎是一种相当明智的方式,因为文件总是相对于彼此分开的两个目录。但这似乎无济于事。它仍在同一目录中寻找文件:

<?php include '../../myfile.php'; ?>

3) 把愚蠢的东西硬编码进去。

然而,这仍然不起作用,即使此处放置的 URL 在浏览器中经过测试并成功加载了正确的文件。它不会将它加载到 include 所在的文件中......

<?php include 'localhost/mywordpresssite/wp-content/themes/mytheme/twitter-feed.php'; ?>

给出错误:

Warning: include(localhost/mywordpresssite/wp-content/themes/mytheme/twitter-feed.php): failed to open stream: No such file or directory in /Applications/MAMP/htdocs/mywordpresssite/wp-content/themes/mytheme/parts/shared/footer.php on line 3

我还能做什么?

最佳答案

您正在尝试通过 URL 来包含。您需要通过文件路径(而不是 uri)include:

<?php
    $stylesheet_root = get_stylesheet_directory();
    include( $stylesheet_root . '/myfile.php' );
?>

阅读更多关于 get_stylesheet_directory() in the docs 的信息.

关于php - 无法包含 Wordpress 主题文件夹中的 PHP 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29627017/

相关文章:

php - 在wordpress的cherry框架中自动更改css

javascript - 尝试使用php函数生成的随机值自动生成QR码

php - 如何删除某些 URL 的 URL 重写

php - Javascript 在 Firefox 插件中通过 POST 发送数据

php - 删除 WordPress 子主题中的父主题排队文件

mysql - Wordpress 4.2.2 更新 - wpdb->insert 失败

php - 给出警告 : number_format() expects parameter 1 to be double, 字符串

jquery - 为什么 Facebook 评论框在调用 jQuery.load 后无法加载?

php - 在 php 中用于俄语的 mb_convert_encoding

javascript - 无法使用 AJAX 表单