Is there a way to get all wordpress post comments in a nested (threaded) php object or array ?
The reason I need a nested object is because this nested object can then be easily passed to a templating engine to output an HTML template with the all comments and their nested replies correctly displayed.
The required / ideal format for the comments object would be like so:
{
"015":{
"comment_content":"Nullam in magna quis libero posuere vestibulum.",
"comment_date":"2016-05-13 00:20:32",
"other_comment_args":"...",
"replies":{
}
},
"837":{
"comment_content":"Nullam in magna quis libero posuere vestibulum.",
"comment_date":"2016-05-13 00:20:32",
"other_comment_args":"...",
"replies":{
"015":{
"comment_content":"Nullam in magna quis libero posuere vestibulum.",
"comment_date":"2016-05-13 00:20:32",
"other_comment_args":"...",
"replies":{
"015":{
"comment_content":"Nullam in magna quis libero posuere vestibulum.",
"comment_date":"2016-05-13 00:20:32",
"other_comment_args":"...",
"replies":{
}
},
"234":{
"comment_content":"Nullam in magna quis libero posuere vestibulum.",
"comment_date":"2016-05-13 00:20:32",
"other_comment_args":"...",
"replies":{
}
}
}
},
"125":{
"comment_content":"Nullam in magna quis libero posuere vestibulum.",
"comment_date":"2016-05-13 00:20:32",
"other_comment_args":"...",
"replies":{
}
},
"654":{
"comment_content":"Nullam in magna quis libero posuere vestibulum.",
"comment_date":"2016-05-13 00:20:32",
"other_comment_args":"...",
"replies":{
}
}
}
},
"785":{
"comment_content":"Nullam in magna quis libero posuere vestibulum.",
"comment_date":"2016-05-13 00:20:32",
"other_comment_args":"...",
"replies":{
"015":{
"comment_content":"Nullam in magna quis libero posuere vestibulum.",
"comment_date":"2016-05-13 00:20:32",
"other_comment_args":"...",
"replies":{
}
},
"231":{
"comment_content":"Nullam in magna quis libero posuere vestibulum.",
"comment_date":"2016-05-13 00:20:32",
"other_comment_args":"...",
"replies":{
}
},
"554":{
"comment_content":"Nullam in magna quis libero posuere vestibulum.",
"comment_date":"2016-05-13 00:20:32",
"other_comment_args":"...",
"replies":{
}
}
}
}
}
I have looked into using the 'get_comments' wordpress function. However that returns an array of all comments in a single level. Any child commnets (reply comments) have a "comment_parent" property with the ID of the parent comment. Here's what a print_r of the function's returned value looks like.
[1] => WP_Comment Object
(
[comment_ID] => 5644
[comment_parent] => 0
[comment_post_ID] => 332
[comment_author_email] => commenter@localhost
[comment_date] => 2016-05-13 00:20:32
[comment_content] => i. Nullam in magna quis libero posuere vestibulum. Donec mi leo, elementum ut.
)
[2] => WP_Comment Object
(
[comment_ID] => 8738
[comment_parent] => 5644
[comment_post_ID] => 332
[comment_author_email] => commenter@localhost
[comment_date] => 2016-05-13 00:20:32
[comment_content] => i. Nullam in magna quis libero posuere vestibulum. Donec mi leo, elementum ut.
)
[3] => WP_Comment Object
(
[comment_ID] => 7758
[comment_parent] => 5644
[comment_post_ID] => 332
[comment_author_email] => commenter@localhost
[comment_date] => 2016-05-13 00:20:32
[comment_content] => i. Nullam in magna quis libero posuere vestibulum. Donec mi leo, elementum ut.
)
A custom comment walker may be helpful, however I've not come across any examples where a comment walker would populate a nested object with comment objects.
Thank you in advance.
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire