Wordpress主题开发:调用未定义的函数WP_Query()

Gil*_*ian 1 php wordpress-theming

我正在开发我自己的Wordpress主题,我在尝试获取自定义帖子("事件")时遇到了我的functions.php文件" 调用未定义函数WP_Query() ".

我已经尝试添加include('wp-load.php'),但没有改变任何东西.

有没有人有同样的问题?已经做了一些研究,但没有找到解决我问题的任何东西.

这是我的简单代码:

$argsEvents = array('post_type'  => 'event', 'posts_per_page' => '-1');

$result = WP_Query( $argsEvents );

if ( $result->have_posts() ) {

    echo '<ul>';
    while ( $result->have_posts() ) {

        $result->the_post();

        echo '<li>' . get_the_title() . '</li>';
    }
    echo '</ul>';

    wp_reset_postdata();

} else echo "No data";
Run Code Online (Sandbox Code Playgroud)

非常感谢你的帮助!

阿娇

Yur*_*uri 6

你应该在调用WP_Query之前使用new.

更改:

$result = WP_Query( $argsEvents );
Run Code Online (Sandbox Code Playgroud)

至:

$result = new WP_Query( $argsEvents );
Run Code Online (Sandbox Code Playgroud)