[英]Is there a way to get all post comments in wordpress in a nested php object or array?
Is there a way to get all wordpress post comments in a nested (threaded) php object or array ? 有没有办法在嵌套(线程)php对象或数组中获取所有wordpress帖子注释?
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. 我需要一个嵌套对象的原因是因为这个嵌套对象可以很容易地传递给模板引擎输出一个HTML模板,其中包含所有注释并正确显示它们的嵌套回复。
The required / ideal format for the comments object would be like so: comments对象的必需/理想格式如下:
{
"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. 我已经研究过使用'get_comments'wordpress函数。 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. 任何子commnet(回复注释)都有一个“comment_parent”属性,其中包含父注释的ID。 Here's what a print_r of the function's returned value looks like. 这是函数返回值的print_r的样子。
[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. 自定义注释walker可能会有所帮助,但是我没有遇到任何注释walker会用注释对象填充嵌套对象的示例。
Thank you in advance. 先感谢您。
If you use nested comments to make a theme, you can try this API: wp_list_comments 如果您使用嵌套注释来创建主题,则可以尝试以下API:wp_list_comments
wp_list_comments can help you generate a html code include comments data wp_list_comments可以帮助您生成包含注释数据的html代码
please ref: https://codex.wordpress.org/Function_Reference/wp_list_comments 请参考: https : //codex.wordpress.org/Function_Reference/wp_list_comments
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.