get_comment_pages_count()
Counts the number of comment pages for a post (how many pages comments are split into).
Allows counting how many comment pages will result if considering the number of comments per page and their threaded output.
Usually, this function is not used before the start of the comment loop.
The function can be used outside the loop. Then, you need to specify the parameter $comments, and $per_page, $threaded if necessary.
No Hooks.
Returns
Int. The number of comment pages.
Usage
get_comment_pages_count( $comments, $per_page, $threaded );
- $comments(array)
- Array of comment objects. By default, the global variable $wp_query->comments is used.
Default: null - $per_page(int)
- How many comments to display on one page. By default, it is taken from the settings.
Default: null - $threaded(bool)
Whether to consider the threadiness of comments or not. By default, it is taken from the settings. It can be:
- false - do not consider, count each comment.
- true - consider the comment tree, count the entire tree as one comment.
Default: null
Examples
#1 Example of using a function in the loop:
$pages = get_comment_pages_count();
#2 Example of using custom parameters
The example above uses all default values, which are taken from the settings. And this example shows how its parameters work:
// pages of comments if 25 comments per page. $pages = get_comment_pages_count( null, 25 ); // comment pages if you do not divide comments into tree-like type (thread comments). $pages = get_comment_pages_count( null, null, false ); // pages of comments, if 10 per page and they are tree-like (thread comments). $pages = get_comment_pages_count( null, 10, true );
#3 Using a function outside the comment loop
If the function is used outside the comment loop, you need to specify the $comments parameter, which contains an array of comments to be counted. Such an array can be obtained by necessary arguments, using class: WP_Comment_Query{}.
Get comments on the parameters you want and find out how many pages they break down into:
$args = array( // query args here ); $comments_query = new WP_Comment_Query; $comments = $comments_query->query( $args ); $pages = get_comment_pages_count( $comments );
Notes
- Global. WP_Query. $wp_query WordPress Query object.
Changelog
| Since 2.7.0 | Introduced. |