钩子文档

get_page_of_comment

💡 云策文档标注

概述

get_page_of_comment 是一个 WordPress 过滤器钩子,用于修改评论在分页中出现的计算页面。它允许开发者基于评论 ID 和相关参数调整分页逻辑。

关键要点

  • 过滤器名称:get_page_of_comment
  • 主要参数:$page(评论页面整数)、$args(分页参数数组)、$original_args(原始参数数组)、$comment_id(评论 ID 整数)
  • 用途:自定义评论分页计算,例如基于评论类型或深度调整页面显示
  • 相关函数:get_page_of_comment() 用于计算评论页面
  • 版本历史:WordPress 4.4.0 引入,4.7.0 添加 $comment_id 参数

代码示例

add_filter( 'get_page_of_comment', 'custom_comment_page', 10, 4 );
function custom_comment_page( $page, $args, $original_args, $comment_id ) {
    // 基于评论 ID 或其他参数修改 $page
    if ( $comment_id == 123 ) {
        $page = 2; // 强制评论显示在第二页
    }
    return $page;
}

注意事项

  • 参数 $args 包含自动检测的分页设置,$original_args 为传递给函数的原始参数,需注意区分
  • 确保返回值为整数类型,以保持分页一致性
  • 此钩子常用于高级评论分页定制,如处理嵌套评论或特定评论类型

📄 原文内容

Filters the calculated page on which a comment appears.

Parameters

$pageint
Comment page.
$argsarray
Arguments used to calculate pagination. These include arguments auto-detected by the function, based on query vars, system settings, etc. For pristine arguments passed to the function, see $original_args.

  • type string
    Type of comments to count.
  • page int
    Calculated current page.
  • per_page int
    Calculated number of comments per page.
  • max_depth int
    Maximum comment threading depth allowed.

$original_argsarray
Array of arguments passed to the function. Some or all of these may not be set.

  • type string
    Type of comments to count.
  • page int
    Current comment page.
  • per_page int
    Number of comments per page.
  • max_depth int
    Maximum comment threading depth allowed.

$comment_idint
ID of the comment.

Source

return apply_filters( 'get_page_of_comment', (int) $page, $args, $original_args, $comment_id );

Changelog

Version Description
4.7.0 Introduced the $comment_id parameter.
4.4.0 Introduced.