WordPress插件REST API结果格式

时间:2018-11-13 19:07:03

标签: wordpress wordpress-rest-api

我正在开发Wordpress插件来扩展REST API。我设法使其与该代码段一起工作

    $args = array( 
        'numberposts' => -1, 
        'category_name' => 'category_1', 
        'tag__in'=>$tags);
    $posts = get_posts( $args );

我以这种格式获得帖子:

[
  {
    "ID": 2258,
    "post_author": "1",
    "post_date": "2018-11-13 11:24:28",
    "post_date_gmt": "2018-11-13 16:24:28",
    "post_content": "title",
    "post_title": "title",
    "post_excerpt": "",
    "post_status": "publish",
    "comment_status": "open",
    "ping_status": "open",
    "post_password": "",
    "post_name": "title",
    "to_ping": "",
    "pinged": "",
    "post_modified": "2018-11-13 11:24:28",
    "post_modified_gmt": "2018-11-13 16:24:28",
    "post_content_filtered": "",
    "post_parent": 0,
    "guid": "https://example.com/?p=2258",
    "menu_order": 0,
    "post_type": "post",
    "post_mime_type": "",
    "comment_count": "0",
    "filter": "raw"
},
...

但是,我需要输出与标准发布结果相同的格式,如下所示:

[
  {
    "id": 2258,
    "date": "2018-11-13T11:24:28",
    "date_gmt": "2018-11-13T16:24:28",
    "guid": {
        "rendered": "https://example.com/?p=2258"
    },
    "modified": "2018-11-13T11:24:28",
    "modified_gmt": "2018-11-13T16:24:28",
    "slug": "title",
    "status": "publish",
    "type": "post",
    "link": "https://example.com/title/",
    "title": {
        "rendered": "title"
    },
    "content": {
        "rendered": "<p>content of the post</p>\n",
        "protected": false
    },
    "excerpt": {
        "rendered": "<p>content of the post</p>\n",
        "protected": false
    },
    "author": 1,
    "featured_media": 0,
    "comment_status": "open",
    "ping_status": "open",
    "sticky": false,
    "template": "",
    "format": "standard",
    "meta": [],
    "categories": [
        77
    ],
    "tags": [
        79
    ],
    "featured_image_urls": {},
    "appp_media": null,
    "jetpack_featured_media_url": "",
    "_links": {
        "self": [
            {
                "href": ...

我想您已经了解我想要的格式。

如何将到目前为止的内容转换为标准的帖子格式? 我的下一步是尝试研究wordpress API的内部工作原理,我希望通过在此处询问来节省一些时间。我正在使用AppPresser 3。

谢谢!

0 个答案:

没有答案