File: /var/www/dk/wp-content/themes/food/api/comments/r_comments.php
<?php
//echo get_the_ID().'-----------222---------';
//$args = array( 'author_email' => '', 'author_url' => '', 'author__in' => '', 'author__not_in' => '', 'include_unapproved' => '', 'fields' => '','ID' => '', 'comment__in' => '', 'comment__not_in' => '', 'karma' => '', 'number' => '', 'offset' => '', 'no_found_rows' => true, 'orderby' => '', 'order' => 'DESC', 'parent' => '', 'parent__in' => '', 'parent__not_in' => '', 'post_author__in' => '', 'post_author__not_in' => '', 'post_id' => 0, 'post__in' => '', 'post__not_in' => '', 'post_author' => '', 'post_name' => '', 'post_parent' => '', 'post_status' => '', 'post_type' => '', 'status' => 'all', 'type' => '', 'type__in' => '', 'type__not_in' => '', 'user_id' => '', 'search' => '', 'hierarchical' => false, 'count' => false, 'cache_domain' => 'core', 'meta_key' => '', 'meta_value' => '', 'meta_query' => '', 'date_query' => null, // See WP_Date_Query 'update_comment_meta_cache' => true, 'update_comment_post_cache' => false,
// );
// Get only the approved comments
$args = array(
'post_id' => intval($_POST['post_id']), //get_the_ID(),
'status' => 'approve',
);
// The comment Query
$comments_query = new WP_Comment_Query();
$comments = $comments_query->query($args);
$outArr = [];
$subArr = [];
// Comment Loop
if ($comments) {
foreach ($comments as $comment) {
// print_r($comment);
// echo '<p>' . $comment->comment_content . '</p>';
$newItem = new stdClass();
$newItem->comment_ID = $comment->comment_ID;
$newItem->comment_post_ID = $comment->comment_post_ID;
$newItem->post_id = $comment->comment_post_ID;
$newItem->comment_content = $comment->comment_content;
$newItem->comment_author = $comment->comment_author;
$newItem->comment_author_user_id = $comment->user_id;
$newItem->comment_author_email = $comment->comment_author_email;
$newItem->comment_author_url = $comment->comment_author_url;
$newItem->comment_date = $comment->comment_date;
$newItem->comment_approved = $comment->comment_approved;
$newItem->comment_type = $comment->comment_type;
$newItem->comment_parent = $comment->comment_parent;
$newItem->sub_comment = [];
if (intval($comment->comment_parent) > 0) {
$subArr[$comment->comment_parent][] = $newItem;
} else {
$outArr[] = $newItem;
}
}
// print_r($outArr);
// print_r($subArr);
foreach ($outArr as $value) {
$comment_ID = $value->comment_ID;
// print_r($comment_ID);
if (array_key_exists($comment_ID, $subArr)) {
// echo '存在子数组';
$value->sub_comment = $subArr[$comment_ID];
}
}
// print_r($outArr);
}
//else {
// echo 'No comments found.';
//}
$res = $outArr;