(转自may博客)本站代码已经全新更改缩减优化,并去掉调用本博客评论排名,优化css。
Step 1. 增加代码至functions.php
将下面的一段代码添加至functions.php的最下方,系统会自动调取最新前23个评论记录。具体多少个记录可以自行修改。
//最新评论排第一 function jiub($af){ global $wpdb; $queryaf="select comment_author, comment_author_url, comment_date from $wpdb->comments where comment_ID in (select max(comment_ID) from $wpdb->comments where comment_approved='1' and comment_author_url !='' and user_id='0' GROUP BY comment_author_email) ORDER BY comment_date DESC LIMIT $af"; $wally = $wpdb->get_results($queryaf); foreach ($wally as $commentaf){ $tmpy= "<li><img src='https://ws3.sinaimg.cn/large/005BYqpgly1g2399g82rwg300u00uq37.jpg'><a target=\"_blank\" href=\"".$commentaf->comment_author_url."\">".$commentaf->comment_author."</a></li>"; $outputy .= $tmpy; } $outputy = "<ul>".$outputy."</ul>"; echo $outputy ; }
Step 2. 在要展示的版块内容地方添加代码
May使用的是Begin主题,想要展示在banner的下方,所以直接在htdocs/wp-content/themes/begin/template/slider.php最下方添加一下代码。(本站是在这个文件中添加的cms-news.php)
//最新评论排第一 function jiub($af){ global $wpdb; $queryaf="select comment_author, comment_author_url, comment_date from $wpdb->comments where comment_ID in (select max(comment_ID) from $wpdb->comments where comment_approved='1' and comment_author_url !='' and user_id='0' GROUP BY comment_author_email) ORDER BY comment_date DESC LIMIT $af"; $wally = $wpdb->get_results($queryaf); foreach ($wally as $commentaf){ $tmpy= "<li><i class=\"fa fa-spinner fa-pulse\"></i><a target=\"_blank\" href=\"".$commentaf->comment_author_url."\">".$commentaf->comment_author."</a></li>"; $outputy .= $tmpy; } $outputy = "<ul>".$outputy."</ul>"; echo $outputy ; }
经测试发现,电脑端和手机端均能正常显示。欢迎大家踊跃评论哟,你每评论一次就会自动展示在最前面哦。
相关文章
wordpress知更鸟主题添加每一次评论自动排行第一教程
继续阅读
2019年4月27日 下午11:22 1F
来学习一下,感谢了
2019年4月27日 下午11:26 2F
评论出现问题了,刷新也看不到。。。
2019年6月7日 下午1:52 3F
学习一下
2019年8月1日 下午5:11 4F
看看