get_comment_pages_count()
云策文档标注
概述
get_comment_pages_count() 函数用于计算评论分页的总页数,支持自定义评论数组、每页评论数和线程评论设置。它优先使用 WP_Query 中的缓存值以提高性能。
关键要点
- 函数返回评论分页的整数页数,基于传入参数或默认值计算。
- 参数 $comments 可选,默认为 $wp_query->comments;$per_page 可选,默认从 query var、option 或 1 中获取;$threaded 可选,默认从 thread_comments option 获取。
- 如果 page_comments 选项未启用,函数直接返回 1。
- 在循环内可简单调用,循环外需传入评论数组,例如通过 WP_Comment_Query 获取。
代码示例
// 基本用法,使用默认值
$pages = get_comment_pages_count();
// 自定义每页评论数为 25
$pages = get_comment_pages_count( null, 25 );
// 禁用线程评论
$pages = get_comment_pages_count( null, null, false );
// 自定义每页评论数为 10 并启用线程评论
$pages = get_comment_pages_count( null, 10, true );
// 循环外使用,传入自定义评论数组
$args = array(
// 查询参数
);
$comments_query = new WP_Comment_Query;
$comments = $comments_query->query( $args );
$pages = get_comment_pages_count( $comments );
原文内容
Calculates the total number of comment pages.
Parameters
$commentsWP_Comment[]optional-
Array of WP_Comment objects. Defaults to
$wp_query->comments.Default:
null $per_pageintoptional-
Comments per page. Defaults to the value of
comments_per_pagequery var, option of the same name, or 1 (in that order).Default:
null $threadedbooloptional-
Control over flat or threaded comments. Defaults to the value of
thread_commentsoption.Default:
null
Source
function get_comment_pages_count( $comments = null, $per_page = null, $threaded = null ) {
global $wp_query;
if ( null === $comments && null === $per_page && null === $threaded && ! empty( $wp_query->max_num_comment_pages ) ) {
return $wp_query->max_num_comment_pages;
}
if ( ( ! $comments || ! is_array( $comments ) ) && ! empty( $wp_query->comments ) ) {
$comments = $wp_query->comments;
}
if ( empty( $comments ) ) {
return 0;
}
if ( ! get_option( 'page_comments' ) ) {
return 1;
}
if ( ! isset( $per_page ) ) {
$per_page = (int) get_query_var( 'comments_per_page' );
}
if ( 0 === $per_page ) {
$per_page = (int) get_option( 'comments_per_page' );
}
if ( 0 === $per_page ) {
return 1;
}
if ( ! isset( $threaded ) ) {
$threaded = get_option( 'thread_comments' );
}
if ( $threaded ) {
$walker = new Walker_Comment();
$count = ceil( $walker->get_number_of_root_elements( $comments ) / $per_page );
} else {
$count = ceil( count( $comments ) / $per_page );
}
return (int) $count;
}
Changelog
| Version | Description |
|---|---|
| 2.7.0 | Introduced. |
Skip to note 4 content
Codex
Basic Usage
This function can be used within the loop like this:
Skip to note 5 content
Codex
Number of comments per page
This will use the defaults for the number of comments per page and threading. You can use custom values like this:
// Show 25 comments per page. $pages = get_comment_pages_count( null, 25 ); // Don't thread comments. $pages = get_comment_pages_count( null, null, false ); // Show 10 comments per page, use threading. $pages = get_comment_pages_count( null, 10, true );Skip to note 6 content
Codex
Outside the loop
When inside the loop, you can just pass null as the value for the $comment parameter, as shown above. You can also use the function outside the loop, but you need to pass in the array of comments. For example, you may perform a custom comment query using
WP_Comment_Query:$args = array( // query args here ); $comments_query = new WP_Comment_Query; $comments = $comments_query->query( $args ); $pages = get_comment_pages_count( $comments );