Wordpress REST API:如何在 WP REST API JSON 文件中获取“纯文字”内容?

Leo*_*Guo 5 php api rest wordpress json

我正在使用 WP REST API 从我的网站检索数据,例如,从这个 http:http://localhost:8888/wordpress/wp-json/wp/v2/posts/42我可以得到帖子 42 的信息,但在内容部分,它显示如下

在此处输入图片说明

实际帖子的格式为:

这是一个测试博客+[图片]+这是一个测试博客+[图片]

我想要的内容部分只是单词,而不是图像的信息,我该怎么做才能实现这一点?

以及为此内容部分返回的 WP REST API 格式是什么?我从网站上读到,它说它是“对象”。我是 WP 新手。

Kos*_*sso 1

您需要连接rest_api_init并修改您需要的内容。

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;
}
Run Code Online (Sandbox Code Playgroud)

然后您应该会在 JSON 中看到额外的数据。