wp_count_comments()获取所有的评论总数

目录

描述

Retrieve a total comment count for a site or post.

wp_count_comments() 描述

用法

 <?php wp_count_commentspost_id ); ?> 

wp_count_comments() 用法

参数

$post_id

(integer) (可选) The post ID to retrieve a comment count for.

默认值: None

wp_count_comments() 参数

示例

Default Usage

Retrieve comment count for a site.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */

<?php
$comments_count = wp_count_comments();
echo "Comments for site <br />";
echo "Comments in moderation: " . $comments_count->moderated . "<br />"; 
echo "Comments approved: " . $comments_count->approved . "<br />";
echo "Comments in Spam: " . $comments_count->spam . "<br />";
echo "Comments in Trash: " . $comments_count->trash . "<br />";
echo "Total Comments: " . $comments_count->total_comments . "<br />";
?>

Retrieve Comment Count for a Post

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */

<?php
$comments_count = wp_count_comments( 2492 );
echo "Comments for post 2492 <br />";
echo "Comments in moderation: " . $comments_count->moderated . "<br />"; 
echo "Comments approved: " . $comments_count->approved . "<br />";
echo "Comments in Spam: " . $comments_count->spam . "<br />";
echo "Comments in Trash: " . $comments_count->trash . "<br />";
echo "Total Comments: " . $comments_count->total_comments . "<br />";
?>

wp_count_comments() 示例

源文件

wp_count_comments() 函数的代码位于 wp-includes/comment.php.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
/**
 * Retrieve total comments for blog or single post.
 *
 * The properties of the returned object contain the 'moderated', 'approved',
 * and spam comments for either the entire blog or single post. Those properties
 * contain the amount of comments that match the status. The 'total_comments'
 * property contains the integer of total comments.
 *
 * The comment stats are cached and then retrieved, if they already exist in the
 * cache.
 *
 * @since 2.5.0
 *
 * @global wpdb $wpdb
 *
 * @param int $post_id Optional. Post ID.
 * @return object|array Comment stats.
 */
function wp_count_comments( $post_id = 0 ) {
	global $wpdb;

	$post_id = (int) $post_id;

	/**
	 * Filter the comments count for a given post.
	 *
	 * @since 2.7.0
	 *
	 * @param array $count   An empty array.
	 * @param int   $post_id The post ID.
	 */
	$stats = apply_filters( 'wp_count_comments', array(), $post_id );
	if ( !empty($stats) )
		return $stats;

	$count = wp_cache_get("comments-{$post_id}", 'counts');

	if ( false !== $count )
		return $count;

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

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

	$total = 0;
	$approved = array('0' => 'moderated', '1' => 'approved', 'spam' => 'spam', 'trash' => 'trash', 'post-trashed' => 'post-trashed');
	foreach ( (array) $count as $row ) {
		// Don't count post-trashed toward totals
		if ( 'post-trashed' != $row['comment_approved'] && 'trash' != $row['comment_approved'] )
			$total += $row['num_comments'];
		if ( isset( $approved[$row['comment_approved']] ) )
			$stats[$approved[$row['comment_approved']]] = $row['num_comments'];
	}

	$stats['total_comments'] = $total;
	foreach ( $approved as $key ) {
		if ( empty($stats[$key]) )
			$stats[$key] = 0;
	}

	$stats = (object) $stats;
	wp_cache_set("comments-{$post_id}", $stats, 'counts');

	return $stats;
}

wp_count_comments() 源文件

相关