get_comment_pages_count

Calculate the total number of comment pages.

Description

(int) get_comment_pages_count( (constant) $comments = null, (constant) $per_page = null, (null) $threaded = null ); 

Returns (int)

Number of comment pages.

Parameters (3)

0. $comments — Optional. (constant) => null
Array of WP_Comment objects. Defaults to $wp_query->comments
1. $per_page — Optional. (constant) => null
Comments per page.
2. $threaded — Optional. (null) => null
Control over flat or threaded comments.

Usage

  1. if ( !function_exists( 'get_comment_pages_count' ) ) { 
  2. require_once ABSPATH . WPINC . '/comment.php'; 
  3.  
  4. // Optional array of WP_Comment objects. Defaults to $wp_query->comments 
  5. $comments = null; 
  6.  
  7. // Optional comments per page. 
  8. $per_page = null; 
  9.  
  10. // Optional control over flat or threaded comments. 
  11. $threaded = null; 
  12.  
  13. // NOTICE! Understand what this does before running. 
  14. $result = get_comment_pages_count($comments, $per_page, $threaded); 
  15.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/comment.php  
  1. function get_comment_pages_count( $comments = null, $per_page = null, $threaded = null ) { 
  2. global $wp_query; 
  3.  
  4. if ( null === $comments && null === $per_page && null === $threaded && !empty($wp_query->max_num_comment_pages) ) 
  5. return $wp_query->max_num_comment_pages; 
  6.  
  7. if ( ( ! $comments || ! is_array( $comments ) ) && ! empty( $wp_query->comments ) ) 
  8. $comments = $wp_query->comments; 
  9.  
  10. if ( empty($comments) ) 
  11. return 0; 
  12.  
  13. if ( ! get_option( 'page_comments' ) ) { 
  14. return 1; 
  15.  
  16. if ( !isset($per_page) ) 
  17. $per_page = (int) get_query_var('comments_per_page'); 
  18. if ( 0 === $per_page ) 
  19. $per_page = (int) get_option('comments_per_page'); 
  20. if ( 0 === $per_page ) 
  21. return 1; 
  22.  
  23. if ( !isset($threaded) ) 
  24. $threaded = get_option('thread_comments'); 
  25.  
  26. if ( $threaded ) { 
  27. $walker = new Walker_Comment; 
  28. $count = ceil( $walker->get_number_of_root_elements( $comments ) / $per_page ); 
  29. } else { 
  30. $count = ceil( count( $comments ) / $per_page ); 
  31.  
  32. return $count;