大家有沒有注意到本部落格在評論部分的「目前共有 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、刷新一下,看看成果吧!