Fatal error: Call to a member function get() on a non-object
程序员文章站
2024-01-01 08:33:04
...
在自己代码的函数中使用 require(BASE_PATH . ‘wp-load.php’); 引入 WordPress 核心代码,然后执行 WP_Query 获取特定的日志,然后就发生下面的错误:
Fatal error: Call to a member function get() on a non-object in [path to site]\site\wp-includes\query.php on line 27
这是因为全局变量不正确使用引起的问题,默认 $wp_query 是在 wp-setting.php 中定义的,因为是定义在最外层,所以 $wp_query 自然就是一个全局的变量。但是这里在自己的函数中通过 require(BASE_PATH . ‘wp-load.php’); 引入 WordPress 核心代码,$wp_query 反而变成这个函数中的本地变量,所以其他代码调用 $wp_query 的时候,就发生了错误。
解决方法是:在 require(BASE_PATH . ‘wp-load.php’); 代码上面加入以下代码,预先将 $wp_query 定义为全局变量:
define('WP_USE_THEMES', false); global $wp, $wp_query, $wp_the_query, $wp_rewrite, $wp_did_header;
推荐阅读
-
Fatal error: Call to undefined function: iconv() in d:wwwPiaokouwwwrootpiaok解决方案
-
Fatal error: Call to undefined function mssql_connect()解决办法
-
Fatal error: Call to a member function get() on a non-object
-
Call to a member function on a non-object,分不够再加
-
PHP Fatal error: Call to undefined function bcmul()_PHP教程
-
PHP对象编程有关问题,Call to a member function hello() on a non-object
-
PHP FATAL ERROR: CALL TO UNDEFINED FUNCTION BCMUL()解决办法_PHP
-
PHP对象编程有关问题,Call to a member function hello() on a non-object
-
【LAMP】Fatal error: Call to undefined function mb_detect_encoding(),该怎么解决
-
PHP Fatal error: Call to undefined function bcmul()_PHP教程