返回介绍

wp_count_comments()

发布于 2017-09-11 11:43:25 字数 4583 浏览 959 评论 0 收藏 0

wp_count_comments( int $post_id )

Retrieve total comments for blog or single post.


description

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.


参数

$post_id

(int) (Optional) Post ID.


返回值

(object|array) Comment stats.


源代码

File: wp-includes/comment.php

function wp_count_comments( $post_id = 0 ) {
	$post_id = (int) $post_id;

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

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

	$stats = get_comment_count( $post_id );
	$stats['moderated'] = $stats['awaiting_moderation'];
	unset( $stats['awaiting_moderation'] );

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

	return $stats_object;
}

更新日志

Versiondescription
2.5.0Introduced.

相关函数

Uses

  • wp-includes/cache.php: wp_cache_get()
  • wp-includes/cache.php: wp_cache_set()
  • wp-includes/plugin.php: apply_filters()
  • wp-includes/comment.php: wp_count_comments
  • wp-includes/comment.php: get_comment_count()

Used By

  • wp-admin/includes/dashboard.php: wp_dashboard_right_now()
  • wp-admin/includes/ajax-actions.php: _wp_ajax_delete_comment_response()
  • wp-admin/includes/ajax-actions.php: wp_ajax_replyto_comment()
  • wp-admin/includes/class-wp-comments-list-table.php: WP_Comments_List_Table::get_views()
  • wp-includes/admin-bar.php: wp_admin_bar_comments_menu()
  • wp-includes/class-wp-xmlrpc-server.php: wp_xmlrpc_server::wp_getCommentCount()
  • Show 1 more used by Hide more used by

User Contributed Notes

  1. Skip to note content You must log in to vote on the helpfulness of this noteVote results for this note: 0You must log in to vote on the helpfulness of this note Contributed by Codex

    Default usage
    Retrieve comment count for a site.

    
    <?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 />";
    ?>
    
    
  2. Retrieve comment count for a post

    
    <?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 />";
    ?>
    
    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文