钩子文档

comments_per_page

💡 云策文档标注

概述

comments_per_page 是一个 WordPress 过滤器,用于控制评论列表表格中每页显示的评论数量。它允许开发者根据评论状态(如垃圾评论)动态调整分页设置。

关键要点

  • 过滤器名称:comments_per_page
  • 参数:$comments_per_page(整数,每页评论数)和 $comment_status(字符串,评论状态,默认 'All')
  • 用途:在后台评论管理页面中,自定义每页显示的评论数量,常用于优化垃圾评论等特定状态的显示
  • 相关函数:WP_Comments_List_Table::get_per_page() 和 WP_Screen::render_per_page_options()
  • 引入版本:WordPress 2.6.0

代码示例

/**
 * Increase comments-per-page limit when viewing spam comments.
 *
 * @see WP_Comments_List_Table::get_per_page()
 *
 * @param int    $comments_per_page The number of comments to list per page.
 * @param string $comment_status    The current comment status view. Default is 'all'.
 * @return int The filtered number of comments to list per page.
 */
function wpdocs_spam_comments_per_page( $comments_per_page, $comment_status ) {
	if ( 'spam' == $comment_status ) {
		$comments_per_page = 50;
	}
	return $comments_per_page;
}
add_filter( 'comments_per_page', 'wpdocs_spam_comments_per_page', 10, 2 );

📄 原文内容

Filters the number of comments listed per page in the comments list table.

Parameters

$comments_per_pageint
The number of comments to list per page.
$comment_statusstring
The comment status name. Default 'All'.

Source

return apply_filters( 'comments_per_page', $comments_per_page, $comment_status );

Changelog

Version Description
2.6.0 Introduced.

User Contributed Notes

  1. Skip to note 2 content

    /**
     * Increase comments-per-page limit when viewing spam comments.
     *
     * @see WP_Comments_List_Table::get_per_page()
     *
     * @param int    $comments_per_page The number of comments to list per page.
     * @param string $comment_status    The current comment status view. Default is 'all'.
     * @return int The filtered number of comments to list per page.
     */
    function wpdocs_spam_comments_per_page( $comments_per_page, $comment_status ) {
    	if ( 'spam' == $comment_status ) {
    		$comments_per_page = 50;
    	}
    	return $comments_per_page;
    }
    add_filter( 'comments_per_page', 'wpdocs_spam_comments_per_page', 10, 2 );