获取文章评论数及人数

  1. /**
  2. * 获取文章的评论人数
  3. *$postid:文章id
  4. *$which:返回类型(0或1)为0时返回评论人数,为1时返回评论条数
  5. */
  6. function count_comments($postid=0,$which=0) {
  7. $comments = get_comments('status=approve&type=comment&post_id='.$postid); //获取文章的所有评论
  8. if ($comments) {
  9. $i=0; $j=0; $commentusers=array();
  10. foreach ($comments as $comment) {
  11. ++$i;
  12. if ($i==1) { $commentusers[] = $comment->comment_author_email; ++$j; }
  13. if ( !in_array($comment->comment_author_email, $commentusers) ) {
  14. $commentusers[] = $comment->comment_author_email;
  15. ++$j;
  16. }
  17. }
  18. $output = array($j,$i);
  19. $which = ($which == 0) ? 0 : 1;
  20. return $output[$which]; //返回评论人数
  21. }
  22. return 0; //没有评论返回0
  23. }