get_comment_count

函式
get_comment_count ( $post_id = 0 )
引數
  • (int) $post_id Optional. Restrict the comment counts to the given post. Default 0, which indicates that comment counts for the whole site will be retrieved.
    Required:
返回值
  • (int[]) { The number of comments keyed by their status. @type int $approved The number of approved comments. @type int $awaiting_moderation The number of comments awaiting moderation (a.k.a. pending). @type int $spam The number of spam comments. @type int $trash The number of trashed comments. @type int $post-trashed The number of comments for posts that are in the trash. @type int $total_comments The total number of non-trashed comments, including spam. @type int $all The total number of pending or approved comments. }
定義位置
相關方法
get_comment_pages_countget_comment_authorget_comment_linkget_comment_dateget_comment_type
引入
2.0.0
棄用
-

get_comment_count – 這個函式返回一個文章的評論總數。它接受一個可選的文章ID作為引數。

檢索整個網站或單個文章的總評論數。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function get_comment_count( $post_id = 0 ) {
$post_id = (int) $post_id;
$comment_count = array(
'approved' => 0,
'awaiting_moderation' => 0,
'spam' => 0,
'trash' => 0,
'post-trashed' => 0,
'total_comments' => 0,
'all' => 0,
);
$args = array(
'count' => true,
'update_comment_meta_cache' => false,
);
if ( $post_id > 0 ) {
$args['post_id'] = $post_id;
}
$mapping = array(
'approved' => 'approve',
'awaiting_moderation' => 'hold',
'spam' => 'spam',
'trash' => 'trash',
'post-trashed' => 'post-trashed',
);
$comment_count = array();
foreach ( $mapping as $key => $value ) {
$comment_count[ $key ] = get_comments( array_merge( $args, array( 'status' => $value ) ) );
}
$comment_count['all'] = $comment_count['approved'] + $comment_count['awaiting_moderation'];
$comment_count['total_comments'] = $comment_count['all'] + $comment_count['spam'];
return array_map( 'intval', $comment_count );
}
function get_comment_count( $post_id = 0 ) { $post_id = (int) $post_id; $comment_count = array( 'approved' => 0, 'awaiting_moderation' => 0, 'spam' => 0, 'trash' => 0, 'post-trashed' => 0, 'total_comments' => 0, 'all' => 0, ); $args = array( 'count' => true, 'update_comment_meta_cache' => false, ); if ( $post_id > 0 ) { $args['post_id'] = $post_id; } $mapping = array( 'approved' => 'approve', 'awaiting_moderation' => 'hold', 'spam' => 'spam', 'trash' => 'trash', 'post-trashed' => 'post-trashed', ); $comment_count = array(); foreach ( $mapping as $key => $value ) { $comment_count[ $key ] = get_comments( array_merge( $args, array( 'status' => $value ) ) ); } $comment_count['all'] = $comment_count['approved'] + $comment_count['awaiting_moderation']; $comment_count['total_comments'] = $comment_count['all'] + $comment_count['spam']; return array_map( 'intval', $comment_count ); }
function get_comment_count( $post_id = 0 ) {
	$post_id = (int) $post_id;

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

	$args = array(
		'count'                     => true,
		'update_comment_meta_cache' => false,
	);
	if ( $post_id > 0 ) {
		$args['post_id'] = $post_id;
	}
	$mapping       = array(
		'approved'            => 'approve',
		'awaiting_moderation' => 'hold',
		'spam'                => 'spam',
		'trash'               => 'trash',
		'post-trashed'        => 'post-trashed',
	);
	$comment_count = array();
	foreach ( $mapping as $key => $value ) {
		$comment_count[ $key ] = get_comments( array_merge( $args, array( 'status' => $value ) ) );
	}

	$comment_count['all']            = $comment_count['approved'] + $comment_count['awaiting_moderation'];
	$comment_count['total_comments'] = $comment_count['all'] + $comment_count['spam'];

	return array_map( 'intval', $comment_count );
}

常見問題

FAQs
檢視更多 >