2 Ways to Get Article Comments in WordPress
Photo by Pixelcreatures on Pixabay
Comments are used in articles to get questions, feedback, corrections, and more. In this opportunity, Mangcoding will share about 2 Ways to Get Article Comments in WordPress, hopefully, this can help all of you. Without further ado, let’s dive right into it!
I’m sure you’re familiar with the wp_list_comments() function. This function displays comments on the website page.
Why is this function so great and cool?
- First, this function runs a comment loop and assigns a global variable that holds the current depth level of the comment in the loop.
- Second, wp_list_comments allows you to define and choose your own comment template using the callback parameter.
wp_list_comments('callback=my_custom_comment_template');
In my_custom_comment_template(), the global variables $comment_depth and $GLOBALS[‘comment_depth’] will be set.
What if you want to get the comment depth level when the global variable $comment_depth is not available? And you only have the comment ID, for example?
You can use the function below to solve this issue:
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, also use this command:
echo rudr_get_comment_depth( 784 ); // print the depth level of comment 784
That’s the article on 2 Ways to Get Article Comments in WordPress that Mangcoding shared. Hopefully, this will be useful and help you. If you have any constructive criticism or suggestions, feel free to comment or send them via email and Mangcoding’s social media.
Reference : rudrastyh.com