从WP REST API获取单个帖子和多个帖子的不同响应

时间:2017-12-09 19:01:33

标签: wordpress wordpress-rest-api

是否有任何选项可以为WordPress单个帖子和帖子列表获得不同的类型响应? 我的帖子目标列表响应为

[
{"id":1,
"date":"2017-08-20T07:26:55",
"link":"http://localhost/wordpress/2017/08/20/test-post",
"title":{"rendered":"Test post"}
},
{"id":2,
"date":"2017-08-20T07:26:55",
"link":"http://localhost/wordpress/2017/08/20/test-post",
"title":{"rendered":"Test post"}
},
{"id":3,
"date":"2017-08-20T07:26:55",
"link":"http://localhost/wordpress/2017/08/20/test-post",
"title":{"rendered":"Test post"}
},
{"id":4,
"date":"2017-08-20T07:26:55",
"link":"http://localhost/wordpress/2017/08/20/test-post",
"title":{"rendered":"Test post"}
}
]

单个帖子回复

{
  "id": 92,
  "date": "2017-08-20T07:13:42",
  "date_gmt": "2017-08-20T07:13:42",
  "guid": {
    "rendered": "http://devel8/wp-news/?p=1"
  },
  "modified": "2017-08-20T07:13:42",
  "modified_gmt": "2017-08-20T07:13:42",
  "slug": "hello-world-2",
  "status": "publish",
  "type": "post",
  "link": "http://localhost/wordpress/2017/08/20/hello-world-2/",
  "title": {
    "rendered": "Hello world!"
  },
  "content": {
    "rendered": "<p>Welcome to WordPress. This is your first post. Edit or delete it, then start writing!</p>\n",
    "protected": false
  },
  "excerpt": {
    "rendered": "<p>Welcome to WordPress. This is your first post. Edit or delete it, then start writing!</p>\n",
    "protected": false
  },
  "author": 1,
  "featured_media": 0,
  "comment_status": "open",
  "ping_status": "open",
  "sticky": false,
  "template": "",
  "format": "standard",
  "meta": [],
  "categories": [
    1
  ],
  "tags": [],
  .....
  .....
  }
}

注意:使用 register_rest_field() rest_prepare_post 过滤器,我们可以修改两个(单个帖子和多个帖子)的响应但我们需要单独使用响应。

有任何选项可以知道 register_rest_field() get_callback 函数中的多个帖子或单个帖子的请求。

提前致谢。

1 个答案:

答案 0 :(得分:0)

由于我没有从任何一个得到任何响应和解决方案,所以我决定开发一个WordPress插件,它将满足我的要求,处理单个帖子和帖子列表或多个帖子或循环的WP REST API请求响应帖子或帖子组不同,管理员可以从后端控制。

经过长时间的努力,我开发了名为

的插件

One Call – WP REST API Extension

插件的核心功能

  • 自定义和后端控制休息api前缀如'test-api'所在 'wp-json'默认为最初保护api呼叫。
  • 针对列表帖子和单个帖子回复获得不同的响应。
  • 对于帖子列表(多个)通话,您可以控制“one_call”字段 从后端。
  • WordPress发布后端字段过滤选项以显示帖子列表 (帖子循环)。

希望这个插件可以帮助像我这样的人,他们计划使用Ionic,Phonegap,React Native,Framework&amp; amp;计划为WordPress网站开发移动应用程序。和NativeScript等。

相关问题