当我在子菜单中显示3个热门帖子时会出现错误(最大的菜单)(get error when i wana to show 3 numbers of popular posts in submenu (ma

编程入门 行业动态 更新时间:2024-10-26 02:28:31
我在子菜单中显示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 } } ?>

更多推荐

本文发布于:2023-08-07 21:11:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465938.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:我在   时会   出现错误   菜单   热门

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!