简体   繁体   中英

WordPress having trouble working outside of itself

I'm trying to call WordPress functions on an external page. 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'); ?>

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; 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. 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 ($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.

Furthermore, I remove the if / else statement and just try the foreach loop to see the exact error I get.

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.

you need to initialize WordPress before calling any functions. Try requiring wp-config.php into your external page before calling any functions.

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

Looks like you need to reinstall Wordpress . BACKUP YOUR STUFF BEFORE DOING IT.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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