钩子文档

get_comments_number

💡 云策文档标注

概述

get_comments_number 是一个 WordPress 过滤器,用于修改文章评论数量的返回值。它允许开发者自定义评论计数的显示逻辑。

关键要点

  • 过滤器名称:get_comments_number
  • 参数:$comments_number(字符串或整数,表示评论数量)和 $post_id(整数,文章 ID)
  • 用途:在 get_comments_number() 函数中调用,用于过滤评论计数
  • 引入版本:WordPress 1.5.0

代码示例

add_filter( 'get_comments_number', 'custom_comments_number', 10, 2 );
function custom_comments_number( $comments_number, $post_id ) {
    // 自定义逻辑,例如基于文章类型或条件修改评论数量
    if ( get_post_type( $post_id ) == 'custom_post_type' ) {
        $comments_number = '自定义数量';
    }
    return $comments_number;
}

📄 原文内容

Filters the returned comment count for a post.

Parameters

$comments_numberstring|int
A string representing the number of comments a post has, otherwise 0.
$post_idint
Post ID.

Source

return apply_filters( 'get_comments_number', $comments_number, $post_id );

Changelog

Version Description
1.5.0 Introduced.