当我在子菜单中显示3个热门帖子时会出现错误(最大的菜单)(get error when i wana to show 3 numbers of popular posts in submenu (max mega menu))
我想在子菜单(最大的菜单插件)中显示3个热门帖子(wordpress),但是当我尝试这样做时,我收到包含此消息的错误致命错误:在null上调用成员函数get_results()
<?php $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 } } ?>I wana to show 3 numbers of popular posts (wordpress) in sub menu (max mega menu plugin) but when i try to do it , i get a error that contain this message Fatal error: Call to a member function get_results() on null
<?php $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 } } ?>最满意答案
正如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 } } ?>As George has already added into the comments that you need to add global keyword for $wpdb.
Your final code will be:
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 } } ?>更多推荐
发布评论