如何在WordPress REST API中修改自定义帖子类型响应?

问题描述

我正在创建名为console.log(playerList.itemAt(i).playerName) 自定义帖子类型。

我需要更改一些内容,例如第一次获取缩略图而不是获取ID等。我似乎找不到像平常一样添加过滤器的方法,尝试过,但是case-studies似乎不起作用。

这是代码

add_filter

解决方法

WP会产生所有的拇指吗?我认为您不需要更改声明CPT的方式。但在视图上,请使用:

for content in data["content"]:
    content["value"] 

您应该使用wordpress的内置功能调用图片,只需将大小作为第二个参数传递即可。

,

我相信 register_rest_field 函数正是您要找的。调用 rest_api_init 钩子。它允许您通过 REST API 返回自定义数据,但请注意不要弄乱其他插件或您的 UI 可能依赖的现有字段。

add_action('rest_api_init',function () {
  register_rest_field(
    <your post type,'case-studies'>,<name you want the new data to appear under>,array(
    'get_callback' => function (array $post_object) {
      ... do your logic  here
      return whatever you want to be shown in the REST API
    })
  );
});

更多信息:https://developer.wordpress.org/rest-api/extending-the-rest-api/modifying-responses/#adding-custom-fields-to-api-responses