I am using WP REST API to retrieve data from my website, for example, from this http: http://localhost:8888/wordpress/wp-json/wp/v2/posts/42 I can the the info of the post 42, but inside the content section, it shows like this
the actual post is in the format:
this is a test blog +[image]+this is a test blog+[image]
all I want from the content section is just the word, not the image's information, what can I do to achieve this?
and what kind of format WP REST API returned for this content section? I read from the website, it said it's "object". I am new to WP.
You need to hook in to rest_api_init
and modify how you need the content to be.
add_action( 'rest_api_init', function ()
{
register_rest_field(
'post',
'content',
array(
'get_callback' => 'do_raw_shortcodes',
'update_callback' => null,
'schema' => null,
)
);
});
function do_raw_shortcodes( $object, $field_name, $request )
{
global $post;
$post = get_post ($object['id']);
// This is what we currently have...
$output['rendered'] = apply_filters( 'the_content', $post->post_content);
// This includes the shortcodes
$output['_raw'] = $post->post_content;
// Add something custom
$output['foo'] = 'bar';
return $output;
}
You should then see the extra data in the JSON.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.