每评论一次你就会自动排在前面代码2(自用优化版)

wp美化4157阅读模式

(转自may博客)本站代码已经全新更改缩减优化,并去掉调用本博客评论排名,优化css。
每评论一次你就会自动排在前面代码2(自用优化版)

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知更鸟主题添加每一次评论自动排行第一教程

宜家网
  • 本文由 发表于 2021年10月25日 05:21:41
  • 转载请务必保留本文链接:https://www.yjro.com/21612.html
评论  4  访客  4
    • 仙踪小栈
      仙踪小栈 2

      来学习一下,感谢了

      • 仙踪小栈
        仙踪小栈 2

        评论出现问题了,刷新也看不到。。。

        • 杂烩网wwwzahuiwcom
          杂烩网wwwzahuiwcom 1

          学习一下

          • 刘一
            刘一 0

            看看

          发表评论