如何在另一页上使用WP-REST添加自定义字段?

时间:2018-12-18 23:43:26

标签: php wordpress

如何通过WP-Rest实现以下功能。有必要在其他页面上(一个插件的控件,我编写该插件)删除具有自定义帖子类型的select,并在选择之后根据该帖子在字段下进行更新。如果有必要,我可以花一点钱来获得帮助。

我的代码在WP-REST中添加了自定义字段,但这不起作用。

add_action( 'rest_api_init', 'create_api_posts_meta_field' );
function create_api_posts_meta_field() {

    // register_rest_field ( 'name-of-post-type', 'name-of-field-to-return', array-of-callbacks-and-schema() )
    register_rest_field( 'parsing', 'parsing_night_key', array(
           'get_callback'    => 'get_post_meta_for_api',
           'schema'          => null,
        )
    );
}

function get_post_meta_for_api( $object ) {
    //get the id of the post object array
    $post_id = $object['id'];

    //return the post meta
    return get_post_meta( $post_id );
}

0 个答案:

没有答案