简体   繁体   English

WordPress在自身之外无法工作

[英]WordPress having trouble working outside of itself

I'm trying to call WordPress functions on an external page. 我正在尝试在外部页面上调用WordPress函数。 I've been able to get rid of the first hurdle which had the error of: 我已经摆脱了具有以下错误的第一个障碍:

This Plugin Requires WordPress 3.1+ or Greater: Activation Stopped!

By literally disabling everything on my site. 通过从字面上禁用我网站上的所有内容。

Now I am facing another little issue. 现在,我面临另一个小问题。

I've added this requirement statement: <?php require('/path/to/my/wp-blog-header.php'); ?> 我添加了以下需求声明: <?php require('/path/to/my/wp-blog-header.php'); ?> <?php require('/path/to/my/wp-blog-header.php'); ?>

And I get the following error. 我得到以下错误。 Of course this requirement statement is outside of any class. 当然,此要求声明不属于任何类别。

Fatal error: Call to a member function main() on a non-object in /home2/phanime/public_html/wp-includes/functions.php on line 781

I've also looked around before asking, and people said to add the following line: global $wpdb; 在询问之前,我也环顾了周围,人们说要添加以下内容: global $wpdb; and it didn't help, the error still persists. 仍然没有帮助,错误仍然存​​在。

I've also tried simply putting that line of code (the required statement) and the issue still persists. 我也尝试过简单地放入那行代码(必需的语句),问题仍然存在。

As suggested by someone, I'll update this question. 根据某人的建议,我将更新此问题。 I've got the initialization working but now I am not able to use WP_Query or get_posts and they don't return anything. 我已经完成了初始化工作,但是现在我无法使用WP_Query或get_posts,并且它们不返回任何内容。 For example this code I tried 例如我试过的这段代码

$testingQuery = get_posts(array(
            'post_type' => 'anime',
            'order' => 'date'
            ));

Then with an if / else statement I check if the variable is empty or not 然后用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>';





        }

In this case the else statement runs which tells me that the variable is empty or what not, as "Test" is displayed. 在这种情况下,将运行else语句,该语句告诉我变量为空或不为空,如显示“ Test”。

Furthermore, I remove the if / else statement and just try the foreach loop to see the exact error I get. 此外,我删除了if / else语句,然后尝试使用foreach循环查看我得到的确切错误。

And this is the error 这是错误

Invalid argument supplied for foreach() in /home2/asdf/public_html/community/library/asdfTab/Listener.php, line 54:

I've also tried the same thing with WP_Query and the variables are returning empty so I don't know what the exact problem is, but it seems that they are not able to retrieve the posts that I am requesting. 我也尝试过用WP_Query做同样的事情,并且变量返回空值,所以我不知道确切的问题是什么,但是似乎他们无法检索我所请求的帖子。

you need to initialize WordPress before calling any functions. 您需要在调用任何函数之前初始化WordPress。 Try requiring wp-config.php into your external page before calling any functions. 在调用任何函数之前,请尝试在您的外部页面中输入wp-config.php。

put the following in the beginning of your external script 将以下内容放在外部脚本的开头

<?php
require_once("path/to/wp-load.php");

then wp will bootstrap and you can call additional functions 然后wp将引导,您可以调用其他功能

Looks like you need to reinstall Wordpress . 看来您需要重新安装Wordpress BACKUP YOUR STUFF BEFORE DOING IT. 在备份之前,请备份您的文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM