WordPress at a glance

get_comment_pages_count() WP 1.0

Calculate the total number of comment pages.

No Hooks.

Return

Int. Number of comment pages.

Usage

get_comment_pages_count( $comments, $per_page, $threaded );
$comments(WP_Comment[])
Array of WP_Comment objects.
Default: $wp_query->comments
$per_page(int)
Comments per page.
Default: null
$threaded(true/false)
Control over flat or threaded comments.
Default: null

Notes

  • Global. WP_Query. $wp_query

Changelog

Since 2.7.0 Introduced.

Code of get comment pages count: wp-includes/comment.php WP 5.2.3

<?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;
}

Related Functions

From tag: Comments pagination (navigation)

More from tag: Comments

More from category: Comments

No comments
    Hello, !     Log In . Register