You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

能否嵌套WordPress REST API的JSON结果?通过register_rest_field实现嵌套响应

实现嵌套结构的JSON响应(使用register_rest_field)

当然可以实现嵌套结构的JSON响应!用register_rest_field完全能做到,我来给你捋捋具体怎么操作。

先看你当前可能的平级结构代码

假设你现在的代码是类似这样的,返回的是平级的自定义字段:

add_action('rest_api_init', function() {
    register_rest_field('post', 'custom_field', array(
        'get_callback' => function($post) {
            return get_post_meta($post['id'], 'custom_field', true);
        },
        'schema' => array(
            'description' => 'A custom field',
            'type' => 'string',
        ),
    ));

    register_rest_field('post', 'another_field', array(
        'get_callback' => function($post) {
            return get_post_meta($post['id'], 'another_field', true);
        },
        'schema' => array(
            'description' => 'Another custom field',
            'type' => 'string',
        ),
    ));
});

这时返回的JSON是平级的:

{
  "id": 123,
  "custom_field": "value1",
  "another_field": "value2"
}

改成嵌套结构的解决方案

想要得到嵌套格式,只需要把这些字段整合到一个父字段的get_callback里,返回一个关联数组即可。REST API会自动把这个数组转换成JSON对象(也就是嵌套结构)。

示例代码如下:

add_action('rest_api_init', function() {
    register_rest_field('post', 'custom_data', array(
        'get_callback' => function($post) {
            // 在这里组装你想要的嵌套结构
            return array(
                'custom_field' => get_post_meta($post['id'], 'custom_field', true),
                'another_field' => get_post_meta($post['id'], 'another_field', true),
                // 还可以添加更深层次的嵌套
                'deep_nested' => array(
                    'inner_field' => get_post_meta($post['id'], 'inner_field', true),
                    'inner_list' => array('item1', 'item2') // 甚至可以包含数组
                )
            );
        },
        'schema' => array(
            'description' => '嵌套的自定义数据结构',
            'type' => 'object', // 这里要指定类型为object
            'properties' => array(
                'custom_field' => array(
                    'description' => '自定义字段1',
                    'type' => 'string'
                ),
                'another_field' => array(
                    'description' => '自定义字段2',
                    'type' => 'string'
                ),
                'deep_nested' => array(
                    'description' => '深层嵌套对象',
                    'type' => 'object',
                    'properties' => array(
                        'inner_field' => array(
                            'description' => '内部嵌套字段',
                            'type' => 'string'
                        ),
                        'inner_list' => array(
                            'description' => '内部数组',
                            'type' => 'array',
                            'items' => array('type' => 'string')
                        )
                    )
                )
            )
        ),
    ));
});

最终的嵌套JSON响应

调用对应的REST端点(比如/wp-json/wp/v2/posts/123)后,你会得到这样的嵌套结构:

{
  "id": 123,
  "custom_data": {
    "custom_field": "value1",
    "another_field": "value2",
    "deep_nested": {
      "inner_field": "inner_value",
      "inner_list": ["item1", "item2"]
    }
  }
}

注意事项

  • 如果之前注册了单独的平级字段,记得删掉对应的register_rest_field调用,避免重复返回。
  • schema部分一定要正确定义嵌套结构的类型(object或array)和内部属性,这样API文档也能正确显示你的嵌套字段。
  • 测试时可以直接用浏览器访问端点,或者用Postman这类工具,确认返回的JSON结构符合预期。

内容的提问来源于stack exchange,提问作者Atasha

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 07:59:31