Using WordPress ‘get_page_of_comment()’ PHP function

The get_page_of_comment() WordPress PHP function calculates what page number a comment will appear on for comment paging.

Usage

get_page_of_comment( $comment_id, $args );

Parameters

  • $comment_id (int): Required. The comment ID.
  • $args (array): Optional. An array of optional arguments:
    • type (string): Limit paginated comments to those matching a given type. Accepts ‘comment’, ‘trackback’, ‘pingback’, ‘pings’ (trackbacks and pingbacks), or ‘all’. Default ‘all’.
    • per_page (int): Per-page count to use when calculating pagination. Defaults to the value of the ‘comments_per_page’ option.
    • max_depth (int|string): If greater than 1, comment page will be determined for the top-level parent $comment_id. Defaults to the value of the ‘thread_comments_depth’ option.

More information

See WordPress Developer Resources: get_page_of_comment()

Examples

Get the page number of a specific comment

This example retrieves the page number for a comment with a given $comment_id.

$comment_id = 25;
$page_number = get_page_of_comment( $comment_id );
echo "Comment " . $comment_id . " is on page " . $page_number . ".";

Get the page number for a comment with a custom number of comments per page

This example calculates the page number for a comment with a custom $per_page value.

$comment_id = 40;
$args = array( 'per_page' => 10 );
$page_number = get_page_of_comment( $comment_id, $args );
echo "Comment " . $comment_id . " is on page " . $page_number . " with 10 comments per page.";

Get the page number for a trackback or pingback comment

This example shows how to get the page number for a trackback or pingback comment.

$comment_id = 35;
$args = array( 'type' => 'pings' );
$page_number = get_page_of_comment( $comment_id, $args );
echo "Pingback or trackback comment " . $comment_id . " is on page " . $page_number . ".";

Get the page number for a comment considering the top-level parent

This example retrieves the page number for a comment considering the top-level parent.

$comment_id = 55;
$args = array( 'max_depth' => 3 );
$page_number = get_page_of_comment( $comment_id, $args );
echo "Comment " . $comment_id . " with max depth of 3 is on page " . $page_number . ".";

Get the page number for a comment with multiple custom parameters

This example demonstrates how to calculate the page number for a comment using multiple custom parameters.

$comment_id = 60;
$args = array(
    'type' => 'comment',
    'per_page' => 8,
    'max_depth' => 2
);
$page_number = get_page_of_comment( $comment_id, $args );
echo "Comment " . $comment_id . " with custom parameters is on page " . $page_number . ".";