简体   繁体   中英

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 ?

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.

If you use nested comments to make a theme, you can try this API: wp_list_comments

wp_list_comments can help you generate a html code include comments data

please ref: https://codex.wordpress.org/Function_Reference/wp_list_comments

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM