繁体   English   中英

如何从 wp json rest api 获取特定数据

[英]how to get specific data from wp json rest api

我已经在我的 Wordpress 网站上安装了 wp-json-rest-api,我想将我的网站连接到一个 android 应用程序。 在这样的 url 中,它获取所有帖子:

http://mywebsite.com/wp-json/posts

在其中一个 android 页面上,我只想获得帖子的标题,仅此而已。 我的意思是我应该如何在 url 或其他任何地方指定我只想要标题和 ID 而不是帖子的所有内容? 例如,在每个帖子的页面中,我只想获得该帖子的标题、ID 和内容,类似这样,帖子的 ID 为 12:

http://mywebsite.com/wp-json/posts?include=title,id,content/12

但在所有帖子列表等其他页面中,我想要这样的东西:

http://mywebsite.com/wp-json/posts?include=title,id

我该怎么办?

一种方法是创建自定义端点。 通过这种方式,您可以编写自己的函数并通过 REST API 导出其结果。 事实上,REST 文档有一个示例,可以执行您所要求的操作。 请参阅此处: https : //developer.wordpress.org/rest-api/extending-the-rest-api/adding-custom-endpoints/并修改他们的示例以删除作者查询逻辑并返回标题列表!

现在 Wordpress Rest Api 已更新,您可以根据需要获取特定数据。 您需要在 _fields[] 查询中发送该字段例如: http://mywebsite.com//wp-json/wp/v2/posts?_fields[]=id&_fields=title ://mywebsite.com//wp-json/wp/v2/posts?_fields[]=id&_fields=title

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM