wp_count_comments()
wp_count_comments( int $post_id ) 检索整个网站或单个帖子的评论总数。 Ret…
wp_count_comments( int $post_id )
检索整个网站或单个帖子的评论总数。
Retrieves the total comment counts for the whole site or a single post.
目录锚点:#说明#参数#源码#笔记
说明(Description)
如果注释统计信息已经存在于缓存中,则缓存并检索注释统计信息。另请参见#另请参阅get_comment_count():它处理获取实时评论计数。
参数(Parameters)
参数 | 类型 | 说明 |
---|---|---|
$post_id | (int) | 将评论计数限制为给定的帖子。默认值为0,表示将检索整个网站的评论计数。 |
源码(Source)
/** * 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; }
更新版本 | 源码位置 | 使用 | 被使用 |
---|---|---|---|
2.5.0 | wp-includes/comment.php | 7 | 16 |
笔记(Notes)
检索帖子的评论数
类别:WordPress 函数手册、
本文收集自互联网,转载请注明来源。
如有侵权,请联系 wper_net@163.com 删除。
还没有任何评论,赶紧来占个楼吧!