【问题标题】:Is there a way to get all post comments in wordpress in a nested php object or array?有没有办法在嵌套的 php 对象或数组中获取 wordpress 中的所有帖子评论?
【发布时间】:2016-05-13 03:35:52
【问题描述】:

有没有办法将所有 wordpress post cmets 放入一个嵌套(线程)的 php 对象或数组中?

我需要一个嵌套对象的原因是因为这个嵌套对象可以很容易地传递给模板引擎以输出一个 HTML 模板,其中所有 cmets 及其嵌套回复都正确显示。

cmets 对象所需/理想的格式如下:

{
   "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_cmets' wordpress 功能。但是,这会返回一个包含单个级别的所有 cmets 的数组。任何子评论(回复 cmets)都有一个带有父评论 ID 的“comment_parent”属性。这是函数返回值的 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.
    )

自定义评论漫游器可能会有所帮助,但是我没有遇到任何评论漫游器使用评论对象填充嵌套对象的示例。

提前谢谢你。

【问题讨论】:

    标签: php wordpress wordpress-theming


    【解决方案1】:

    如果你使用嵌套的 cmets 制作主题,你可以试试这个 API:wp_list_cmets

    wp_list_cmets可以帮你生成包含cmets数据的html代码

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

    【讨论】:

    • 为了将逻辑与表示标记分开,我们想使用一个诱人的引擎,如 twig、musache 或 rainTpl。这意味着主题创建者可以使用纯 HTML 模板,而不是混合使用 PHP 和 HTML 文件。也许有一种方法可以将“get_cmets”的结果重新格式化为分层的多级嵌套对象。
    猜你喜欢
    • 2020-02-23
    • 2017-07-26
    • 2011-06-17
    • 1970-01-01
    • 2015-10-14
    • 2019-07-30
    • 2016-01-09
    • 2015-03-04
    • 1970-01-01
    相关资源
    最近更新 更多