从wordpress API中检索自定义字段会导致Null

时间:2018-12-01 20:29:08

标签: php wordpress

因此,我正在关注a tutorial,以尝试为帖子创建自定义字段并通过wordpress api对其进行查询。我已经创建了自定义字段,并通过在帖子编辑器上制作一个meta框来验证它可以工作。除了没有使用“ get_the_author_meta”方法之外,本教程几乎是相同的,我将不得不使用“ get_post_method”方法,该方法需要发布ID才能起作用。

function bs_add_custom_rest_fields()                              
{                                                                 
    $bs_author_name_schema = array(                               
        'description'   => 'Name of the post author',             
        'type'          => 'string',                              
        'context'       =>   array( 'view' )                      
    );                                                            

    // registering the bs_author_name field                       
    register_rest_field(                                          
        'post',                                                   
        'bs_author_name',                                         
        array(                                                    
            'get_callback'      => 'bs_get_author_name',          
            'update_callback'   => null,                          
            'schema'            => $bs_author_name_schema         
        )                                                         
    );                                                            
}                                                                 


function bs_get_author_name( $object, $field_name, $request ) {
        return $object->id;
    //* return get_the_author_meta( 'display_name', $object['author'] );  *//
}


add_action( 'rest_api_init', 'bs_add_custom_rest_fields' );

出于任何原因,$ object-> id在api中返回“ null”。我可以将其设置为字符串,并且可以正常工作。 $ object-> id在代码中的任何其他函数中都可以正常工作(未显示)。这可能是怎么回事?

0 个答案:

没有答案