Wordpress 当我想在子菜单(max mega菜单)中显示3个流行帖子时出错

Wordpress 当我想在子菜单(max mega菜单)中显示3个流行帖子时出错,wordpress,Wordpress,我想在子菜单(max mega menu plugin)中显示3个流行帖子(wordpress),但当我尝试这样做时,我得到一个包含此消息的错误 致命错误:在null上调用成员函数get_results() 因为George已经在注释中添加了您需要为$wpdb添加全局关键字的内容 您的最终代码为: global $wpdb; $result = $wpdb->get_results("SELECT comment_count,ID,post_title FROM $wpdb

我想在子菜单(max mega menu plugin)中显示3个流行帖子(wordpress),但当我尝试这样做时,我得到一个包含此消息的错误 致命错误:在null上调用成员函数get_results()



  • 因为George已经在注释中添加了您需要为$wpdb添加全局关键字的内容

    您的最终代码为:

        global $wpdb;
     $result = $wpdb->get_results("SELECT comment_count,ID,post_title FROM $wpdb->posts ORDER BY comment_count DESC LIMIT 0 , 5");
        foreach ($result as $post) {
            setup_postdata($post);
            $postid = $post->ID;
            $title = $post->post_title;
            $commentcount = $post->comment_count;
            if ($commentcount != 0) { ?>
                <li class="maxmenu-topnews">
                    <?php the_post_thumbnail();?>
                    <a href="<?php the_permalink();?>"><?php the_title();?></a>
                </li>
            <?php } }
            ?>
    
    global$wpdb;
    $result=$wpdb->get_results(“从$wpdb中选择评论数量、ID、帖子标题->按评论数量发布订单描述限制0,5”);
    foreach($result as$post){
    设置_postdata($post);
    $postid=$post->ID;
    $title=$post->post\u title;
    $commentcount=$post->comment\u count;
    如果($commentcount!=0){?>
    

  • 您需要将
    $wpdb
    引入函数的本地作用域。例如,您可以使用
    全局$wpdb
        global $wpdb;
     $result = $wpdb->get_results("SELECT comment_count,ID,post_title FROM $wpdb->posts ORDER BY comment_count DESC LIMIT 0 , 5");
        foreach ($result as $post) {
            setup_postdata($post);
            $postid = $post->ID;
            $title = $post->post_title;
            $commentcount = $post->comment_count;
            if ($commentcount != 0) { ?>
                <li class="maxmenu-topnews">
                    <?php the_post_thumbnail();?>
                    <a href="<?php the_permalink();?>"><?php the_title();?></a>
                </li>
            <?php } }
            ?>