[英]Is there a way to get all post comments in wordpress in a nested php object or array?
有沒有辦法在嵌套(線程)php對象或數組中獲取所有wordpress帖子注釋?
我需要一個嵌套對象的原因是因為這個嵌套對象可以很容易地傳遞給模板引擎輸出一個HTML模板,其中包含所有注釋並正確顯示它們的嵌套回復。
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":{
}
}
}
}
}
我已經研究過使用'get_comments'wordpress函數。 但是,它會在單個級別中返回所有注釋的數組。 任何子commnet(回復注釋)都有一個“comment_parent”屬性,其中包含父注釋的ID。 這是函數返回值的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.
)
自定義注釋walker可能會有所幫助,但是我沒有遇到任何注釋walker會用注釋對象填充嵌套對象的示例。
先感謝您。
如果您使用嵌套注釋來創建主題,則可以嘗試以下API:wp_list_comments
wp_list_comments可以幫助您生成包含注釋數據的html代碼
請參考: https : //codex.wordpress.org/Function_Reference/wp_list_comments
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.