有没有办法在嵌套的php对象或数组中的wordpress中获取所有帖子评论?

时间:2016-05-13 03:35:52

标签: php wordpress wordpress-plugin wordpress-theming

有没有办法在嵌套(线程)的php对象或数组中获取所有wordpress帖子评论?

我需要嵌套对象的原因是因为这个嵌套对象可以很容易地传递给模板引擎,以输出HTML模板,其中包含所有注释及其嵌套回复。

评论对象的必需/理想格式如下:

{
   "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会用注释对象填充嵌套对象的示例。

提前谢谢。

1 个答案:

答案 0 :(得分:0)

如果您使用嵌套评论制作主题,则可以尝试此API:wp_list_comments

wp_list_comments可以帮助您生成包含评论数据的HTML代码

请参考:https://codex.wordpress.org/Function_Reference/wp_list_comments