【问题标题】:Wordpress REST API: How to get the "word-only" content in WP REST API JSON File?Wordpress REST API:如何在 WP REST API JSON 文件中获取“纯文字”内容?
【发布时间】:2016-12-30 01:51:23
【问题描述】:

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

实际帖子的格式为:

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

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

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

【问题讨论】:

  • 你得到这个问题的答案了吗?如果是请更新它,我也遇到同样的问题

标签: php json wordpress rest api


【解决方案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;
}

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

【讨论】:

    猜你喜欢
    • 2018-01-09
    • 1970-01-01
    • 1970-01-01
    • 2017-10-02
    • 2016-04-12
    • 1970-01-01
    • 2017-05-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多