我正在尝试在外部页面上调用WordPress函数。我已经能够摆脱第一个有错误的障碍:
This Plugin Requires WordPress 3.1+ or Greater: Activation Stopped!
通过禁用我网站上的所有内容。
现在我面临另一个小问题。
我添加了这个需求声明:<?php require('/path/to/my/wp-blog-header.php'); ?>
我得到以下错误。当然,这个需求声明不属于任何类。
Fatal error: Call to a member function main() on a non-object in /home2/phanime/public_html/wp-includes/functions.php on line 781
在询问之前,我也环顾四周,人们说添加以下行:global $wpdb;
,但没有帮助,错误仍然存在。
我也尝试过简单地放入这行代码(必需的语句),但问题仍然存在。
根据某人的建议,我将更新此问题。我已经完成了初始化,但现在我无法使用WP_Query或get_posts,它们也不会返回任何内容。例如,我尝试了
$testingQuery = get_posts(array(
'post_type' => 'anime',
'order' => 'date'
));
然后用if/else语句检查变量是否为空或不是
if ($testingQuery) {
foreach( $testingQuery as $post ) : setup_postdata($post);
$contents .= '<li id="ourTab" class="profileContent">
<div class="section">AnimeList Coming soon.</div>
</li>';
$contents .= get_the_title();
endforeach;
} else {
$contents .= '<li id="ourTab" class="profileContent">
<div class="section">Test</div>
</li>';
}
在这种情况下,运行else语句,当显示"Test"时,该语句告诉该变量为空或不为空。
此外,我删除了if/else语句,只尝试foreach循环来查看我得到的确切错误。
这就是的错误
Invalid argument supplied for foreach() in /home2/asdf/public_html/community/library/asdfTab/Listener.php, line 54:
我也在WP_Query中尝试过同样的方法,但变量返回为空,所以我不知道确切的问题是什么,但他们似乎无法检索到我请求的帖子。
在调用任何函数之前,您需要初始化WordPress。在调用任何函数之前,请尝试要求wp-config.php进入外部页面。
将以下内容放在外部脚本的开头
<?php
require_once("path/to/wp-load.php");
然后wp将引导,您可以调用其他函数
看来您需要重新安装Wordpress在执行之前备份您的工作。