^ 回到顶部
  • 人生没有定律,每个人都有自己的节奏
  • 本站wordpress建站教程均通过实践后发布,希望对你有帮助
  • 希望你的坚持,都是因为热爱,而不是因为不甘心
  • 8年wordpress建站经验,5星服务品质
  • 那些不愿意让你吃亏的人,才是真正值得你深交的人,也是值得你付出时间的人
  • 阿里云2核2g仅需86元/年

wordpress函数get_comment_count()用法示例

get_comment_count( int $post_id )

The amount of comments in a post or total comments.


描述

A lot like wp_count_comments(), in that they both return comment stats (albeit with different types). The wp_count_comments() actually caches, but this function does not.

新用户2核2g仅需86元/年    领取1153套模板    免费cdn流量包    ¥3188礼包


参数

$post_id

(int)
(Optional)
Comment amount in post if > 0, else total comments blog wide.


返回值

(array) The amount of spam, approved, awaiting moderation, and total comments.


源代码

File: wp-includes/comment.php

function get_comment_count( $post_id = 0 ) {
	global $wpdb;

	$post_id = (int) $post_id;

	$where = '';
	if ( $post_id > 0 ) {
		$where = $wpdb->prepare("WHERE comment_post_ID = %d", $post_id);
	}

	$totals = (array) $wpdb->get_results("
		SELECT comment_approved, COUNT( * ) AS total
		FROM {$wpdb->comments}
		{$where}
		GROUP BY comment_approved
	", ARRAY_A);

	$comment_count = array(
		'approved'            => 0,
		'awaiting_moderation' => 0,
		'spam'                => 0,
		'trash'               => 0,
		'post-trashed'        => 0,
		'total_comments'      => 0,
		'all'                 => 0,
	);

	foreach ( $totals as $row ) {
		switch ( $row['comment_approved'] ) {
			case 'trash':
				$comment_count['trash'] = $row['total'];
				break;
			case 'post-trashed':
				$comment_count['post-trashed'] = $row['total'];
				break;
			case 'spam':
				$comment_count['spam'] = $row['total'];
				$comment_count['total_comments'] += $row['total'];
				break;
			case '1':
				$comment_count['approved'] = $row['total'];
				$comment_count['total_comments'] += $row['total'];
				$comment_count['all'] += $row['total'];
				break;
			case '0':
				$comment_count['awaiting_moderation'] = $row['total'];
				$comment_count['total_comments'] += $row['total'];
				$comment_count['all'] += $row['total'];
				break;
			default:
				break;
		}
	}

	return $comment_count;
}

更新日志

Version 描述
2.0.0 Introduced.

相关函数

Uses

  • wp-includes/wp-db.php:
    wpdb::get_results()
  • wp-includes/wp-db.php:
    wpdb::prepare()

Used By

  • wp-includes/comment.php:
    wp_count_comments()

让小雨知道,这篇文章帮到了你
扫码关注微信公众号zs40086(微搜片)随时随地微信看片,抢先福利电影等你来

热门推荐

如有疑问,请前往问答中心反馈!

反馈