wordpress统计文章评论数功能,把如下代码放到模板所在目录的functions.php里即可:
/**
* 统计文章评论数
* 单篇文章调用:echo comments_users($post->ID);
* 统计全站评论数:echo comments_users($postid, 1);
* @author albert
* @date 2021.07.05
*/
function comments_users($postid=0,$which=0) {
$comments = get_comments('status=approve&type=comment&post_id='.$postid); //获取文章的所有评论
if ($comments) {
$i=0; $j=0; $commentusers=array();
foreach ($comments as $comment) {
++$i;
if ($i==1) { $commentusers[] = $comment->comment_author_email; ++$j; }
if ( !in_array($comment->comment_author_email, $commentusers) ) {
$commentusers[] = $comment->comment_author_email;
++$j;
}
}
$output = array($j,$i);
$which = ($which == 0) ? 0 : 1;
return $output[$which]; //返回评论人数
}
return 0; //没有评论返回 0
}
// 去掉p,<br>标签
remove_filter ( 'the_excerpt' , 'wpautop' ); 上一篇: wordpress获取热门文章的功能代码