2 Ways to Get Comment Depth

1. Get the comment depth by using global variables

I’m sure you know about wp_list_comments() function — it prints the comments on a website page.

Why this functions is cool?

2. Get the comment depth by comment ID

Okay, but how can I get a comment depth level, if global variable $comment_depth is unavailable? If everything we have is comment ID for example.

I think this simple function can help you with that:

function rudr_get_comment_depth( $my_comment_id ) {
	$depth_level = 0;
	while( $my_comment_id > 0  ) { // if you have ideas how we can do it without a loop, please, share it with us in comments
		$my_comment = get_comment( $my_comment_id );
		$my_comment_id = $my_comment->comment_parent;
		$depth_level++;
	}
	return $depth_level;
}

Then:

echo rudr_get_comment_depth( 784 ); // print the depth level of comment 784
Misha Rudrastyh

Misha Rudrastyh

I love WordPress, WooCommerce and Gutenberg so much. 11 yrs of experience.

Need some custom developer help? Get in touch