Advertisements

get_page_of_comment

Definition:
function get_page_of_comment( $comment_ID, $args = array() {}

Calculate what page number a comment will appear on for comment paging.

Parameters

  • int $comment_ID: Comment ID.
  • array $args: Optional args.

Return values

returns:Comment page number or null on error.

Source code

function get_page_of_comment( $comment_ID, $args = array() ) {

	global $wpdb;



	if ( !$comment = get_comment( $comment_ID ) )

		return;



	$defaults = array( 'type' => 'all', 'page' => '', 'per_page' => '', 'max_depth' => '' );

	$args = wp_parse_args( $args, $defaults );



	if ( '' === $args['per_page'] && get_option('page_comments') )

		$args['per_page'] = get_query_var('comments_per_page');

	if ( empty($args['per_page']) ) {

		$args['per_page'] = 0;

		$args['page'] = 0;

	}

	if ( $args['per_page'] < 1 )

		return 1;



	if ( '' === $args['max_depth'] ) {

		if ( get_option('thread_comments') )

			$args['max_depth'] = get_option('thread_comments_depth');

		else

			$args['max_depth'] = -1;

	}



	// Find this comment's top level parent if threading is enabled

	if ( $args['max_depth'] > 1 && 0 != $comment->comment_parent )

		return get_page_of_comment( $comment->comment_parent, $args );



	$allowedtypes = array(

		'comment' => '',

		'pingback' => 'pingback',

		'trackback' => 'trackback',

	);



	$comtypewhere = ( 'all' != $args['type'] && isset($allowedtypes[$args['type']]) ) ? " AND comment_type = '" . $allowedtypes[$args['type']] . "'" : '';



	// Count comments older than this one

	$oldercoms = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(comment_ID) FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_parent = 0 AND comment_approved = '1' AND comment_date_gmt < '%s'" . $comtypewhere, $comment->comment_post_ID, $comment->comment_date_gmt ) );



	// No older comments? Then it's page #1.

	if ( 0 == $oldercoms )

		return 1;



	// Divide comments older than this one by comments per page to get this comment's page number

	return ceil( ( $oldercoms + 1 ) / $args['per_page'] );

}

1540

Advertisements

No comments yet... Be the first to leave a reply!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: