如何在Wordpress中的自定义帖子类型的JSON响应中显示自定义字段?

时间:2017-02-19 10:48:51

标签: php wordpress

我想输出WP Job Manager插件的job_listing的自定义字段。 我试过这篇文章:https://wordpress.org/support/topic/rest-api-integration-with-wp-job-manager/并且该字段始终为空。

我尝试返回一个硬编码字符串,但该字段再次为空。

add_action('rest_api_init', function() {
    register_rest_field( 'job_listing', 'geolocation_city', array(
        'get_callback' => function( $job_listing ) {
            return "YO";
        },
        'update_callback' => null,
        'schema' => null
    ));
} );

enter image description here

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。我需要指定模式才能使其工作:

add_action('rest_api_init', function() {
    register_rest_field( 'job_listing', 'geolocation_city', array(
        'get_callback' => function($job_listing) {
            return 'YO';
        },
        'update_callback' => null,
        'schema' => array(
            'description' => __('Some'),
            'type'        => 'string'
        ),
    ));
});