大家有没有注意到本博客在评论部分的「目前共有 2 条评论,双方以 1:1 暂时持平」这样的比分呢?今天就教大家怎么添加这个有趣的评论比分小功能
介绍
这个小功能会根据每篇文章的评论人数来判定显示的下面的内容:
访客以XX大幅领先博主XX
访客以XX暂时领先博主XX
博主以XX大幅领先访客XX
博主以XX暂时领先访客XX
目前共有X条评论,双方以XX:XX暂时持平
教程
1、从WordPress控制台打开「外观」-「主题编辑器」-「评论 (comments.php)」
2、在类似 <h3 id="comments"><?php the_title(); ?>:目前有<?php comments_number('', '1 条留言', '% 条留言' ); ?></h3>
的地方添加下列代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<h3><?php $admin_email = get_bloginfo('admin_email'); $str = "SELECT COUNT(*) FROM $wpdb->comments WHERE comment_post_ID = $post->ID AND comment_approved = '1' AND comment_type = '' AND comment_author_email"; $count_t = $post->comment_count; $count_v = $wpdb->get_var("$str != '$admin_email'"); $count_h = $wpdb->get_var("$str = '$admin_email'"); if($count_v > $count_h){ if($count_v - $count_h >= 5){ echo "访客以 ".$count_v.":".$count_h." 大幅领先博主"; }else if($count_v - $count_h < 5){ echo "访客以 ".$count_v.":".$count_h." 暂时领先博主"; } }else if($count_v < $count_h){ if($count_h - $count_v >= 5){ echo "博主以 ".$count_h.":".$count_v." 大幅领先访客"; }else if($count_h - $count_v < 5){ echo "博主以 ".$count_h.":".$count_v." 暂时领先访客"; } }else if($count_v==$count_h){ if($count_t != 0){ echo "双方以 ".$count_v, ":".$count_h." 暂时持平"; } } ?></h3> |
3、刷新一下,看看成果吧!