以前并不知道如何获取文章的评论数量,只是知道通过get_comments_number函数来解决问题,不过问题也比较多,根据官方显示,这个函数是获取文章所有评论数量:

  • get_comments_number() 函数介绍
  • 函数用法
  • Retrieves the amount of comments a post has.

  • 功能介绍
  • 暂无数据
  • 参数介绍
  • $post_id
    (int|WP_Post) (Optional) Post ID or WP_Post object. Default is the global $post.
  • 返回值
  • (string|int) If the post exists, a numeric string representing the number of comments the post has, otherwise 0.
  • 但是我需要的仅仅是经过审核的也就是通过的评论数量,那么可能就需要别的函数解决了。

    如果你正好也有这样的问题。那么可以考虑使用下面的函数:

  • wp_count_comments() 函数介绍
  • 函数用法
  • Retrieves the total comment counts for the whole site or a single post.

  • 功能介绍
  • The comment stats are cached and then retrieved, if they already exist in the cache.

    Top ↑

      get_comment_count(): Which handles fetching the live comment counts.
  • 参数介绍
  • $post_id
    (int) (Optional) Restrict the comment counts to the given post. Default 0, which indicates that comment counts for the whole site will be retrieved.
  • 返回值
  • (stdClass) The number of comments keyed by their status.

    'approved'(int) The number of approved comments.

    'moderated'(int) The number of comments awaiting moderation (a.k.a. pending).

    'spam'(int) The number of spam comments.

    'trash'(int) The number of trashed comments.

    'post-trashed'(int) The number of comments for posts that are in the trash.

    'total_comments'(int) The total number of non-trashed comments, including spam.

    'all'(int) The total number of pending or approved comments.

  • 根据官方的解释,通过这个函数我们可以获取指定状态的评论数量,下面是一个来自官方的例子:

    这个函数还是很好用的,至少不用像之前的函数那么麻烦了,感兴趣的朋友可以自己去尝试一下看看效果。