如何将帖子中定义的自定义字段添加到wordpress中的其余API响应中

时间:2017-05-15 18:32:55

标签: wordpress wordpress-rest-api

我想在不使用任何插件的情况下执行此操作,因为这些都是核心wordpress功能(自定义字段和REST API)。以下是自定义字段的文档供参考:

https://codex.wordpress.org/Using_Custom_Fields

以下是我的wordpress安装的截图:

wordpress custom fields

以下是目前帖子的API响应:

{
    "_links": {
        "about": [
            {
                "href": "http://example.com/wp-json/wp/v2/types/post"
            }
        ],
        "author": [
            {
                "embeddable": true,
                "href": "http://example.com/wp-json/wp/v2/users/1"
            }
        ],
        "collection": [
            {
                "href": "http://example.com/wp-json/wp/v2/posts"
            }
        ],
        "curies": [
            {
                "href": "https://api.w.org/{rel}",
                "name": "wp",
                "templated": true
            }
        ],
        "replies": [
            {
                "embeddable": true,
                "href": "http://example.com/wp-json/wp/v2/comments?post=21"
            }
        ],
        "self": [
            {
                "href": "http://example.com/wp-json/wp/v2/posts/21"
            }
        ],
        "version-history": [
            {
                "href": "http://example.com/wp-json/wp/v2/posts/21/revisions"
            }
        ],
        "wp:attachment": [
            {
                "href": "http://example.com/wp-json/wp/v2/media?parent=21"
            }
        ],
        "wp:featuredmedia": [
            {
                "embeddable": true,
                "href": "http://example.com/wp-json/wp/v2/media/23"
            }
        ],
        "wp:term": [
            {
                "embeddable": true,
                "href": "http://example.com/wp-json/wp/v2/categories?post=21",
                "taxonomy": "category"
            },
            {
                "embeddable": true,
                "href": "http://example.com/wp-json/wp/v2/tags?post=21",
                "taxonomy": "post_tag"
            }
        ]
    },
    "author": 1,
    "categories": [
        5,
        4
    ],
    "comment_status": "open",
    "content": {
        "protected": false,
        "rendered": ""
    },
    "date": "2017-05-14T15:25:33",
    "date_gmt": "2017-05-14T15:25:33",
    "excerpt": {
        "protected": false,
        "rendered": ""
    },
    "featured_media": 23,
    "format": "standard",
    "guid": {
        "rendered": "http://example.com/?p=21"
    },
    "id": 21,
    "link": "http://example.com/2017/05/14/post/",
    "meta": [],
    "modified": "2017-05-15T18:17:34",
    "modified_gmt": "2017-05-15T18:17:34",
    "ping_status": "open",
    "slug": "",
    "sticky": false,
    "tags": [],
    "template": "",
    "title": {
        "rendered": ""
    },
    "type": "post"
}

如果它可能是相关的,这是我的活动插件:

list of active wordpress plugins: Enable Media Replace, S3 Uploads

非常感谢任何帮助。谢谢!

4 个答案:

答案 0 :(得分:13)

首先,您需要register_rest_fields在WP REST API JSON响应中添加自定义端点

add_action( 'rest_api_init', 'add_custom_fields' );
function add_custom_fields() {
register_rest_field(
'post', 
'custom_fields', //New Field Name in JSON RESPONSEs
array(
    'get_callback'    => 'get_custom_fields', // custom function name 
    'update_callback' => null,
    'schema'          => null,
     )
);
}

然后将您的功能定义为get custom fields

function get_custom_fields( $object, $field_name, $request ) {
//your code goes here
return $customfieldvalue;
}

在本地网站上测试

enter image description here

答案 1 :(得分:0)

我发现REST API Custom Fields插件对此很有用。

答案 2 :(得分:-1)

只需将其添加到CMS中,然后它就可以在WP REST API中使用。

答案 3 :(得分:-1)

将此插件用于此解决方案: https://wordpress.org/plugins/acf-to-rest-api/,因为它允许您通过https://github.com/airesvsg/acf-to-rest-api#endpoints此处列出的新端点将高级自定义字段公开到其自己的JSON响应中。然后,您可以forkJoin观察两个对象(如果使用Angular),并根据您的目的使用组合的响应。