Another simple solution that is working perfect for me now.
You can add the following function on functions.php
or fields.php
Using ACF getFields
before sending a rest request. You can add this to any special page rest_prepare_page
or rest_prepare_post
.
The ACF data will be in the json response with key acf
// add this to functions.php
//register acf fields to Wordpress API
//https://support.advancedcustomfields.com/forums/topic/json-rest-api-and-acf/
function acf_to_rest_api($response, $post, $request) {
if (!function_exists('get_fields')) return $response;
if (isset($post)) {
$acf = get_fields($post->id);
$response->data['acf'] = $acf;
}
return $response;
}
add_filter('rest_prepare_post', 'acf_to_rest_api', 10, 3);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…