get_comment_pages_count( WP_Comment[] $comments = null, int $per_page = null, bool $threaded = null )
Calculate the total number of comment pages.
Description Description
Parameters Parameters
- $comments
-
(WP_Comment[]) (Optional) Array of WP_Comment objects. Defaults to $wp_query->comments.
Default value: null
- $per_page
-
(int) (Optional) Comments per page.
Default value: null
- $threaded
-
(bool) (Optional) Control over flat or threaded comments.
Default value: null
Return Return
(int) Number of comment pages.
Source Source
File: wp-includes/comment.php
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 $count; }
Expand full source code Collapse full source code View on Trac
Changelog Changelog
Version | Description |
---|---|
2.7.0 | Introduced. |
User Contributed Notes User Contributed Notes
You must log in before being able to contribute a note or feedback.
Basic Usage
This function can be used within the loop like this:
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:
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
: