Using WordPress ‘get_comment_pages_count()’ PHP function

The get_comment_pages_count() WordPress PHP function calculates the total number of comment pages.

Usage

A generic example of using the function:

$pages = get_comment_pages_count();

Parameters

  • $comments (WP_Comment array, optional): Array of WP_Comment objects. Defaults to $wp_query->comments. Default: null.
  • $per_page (int, optional): Comments per page. Defaults to the value of comments_per_page query var, option of the same name, or 1 (in that order). Default: null.
  • $threaded (bool, optional): Control over flat or threaded comments. Defaults to the value of thread_comments option. Default: null.

More information

See WordPress Developer Resources: get_comment_pages_count()

Examples

Basic Usage

This function can be used within the loop like this:

$pages = get_comment_pages_count();

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);

Outside the Loop

When inside the loop, you can just pass null as the value for the $comments 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);