简体   繁体   English

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

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

i've installed wp-json-rest-api on my Wordpress website and i want to connect my website to an android app.我已经在我的 Wordpress 网站上安装了 wp-json-rest-api,我想将我的网站连接到一个 android 应用程序。 In such a url, it gets all the posts:在这样的 url 中,它获取所有帖子:

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

On one of the android pages, i want to get ONLY title of posts, nothing more.在其中一个 android 页面上,我只想获得帖子的标题,仅此而已。 I mean how should i specify in the url or anywhere else that i just want titles and IDs not all contents of posts?我的意思是我应该如何在 url 或其他任何地方指定我只想要标题和 ID 而不是帖子的所有内容? for example in page of each post, i just want to get title, id and content of that post, something like this, where id of the post is 12:例如,在每个帖子的页面中,我只想获得该帖子的标题、ID 和内容,类似这样,帖子的 ID 为 12:

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

but in other pages like list of all posts, i want something like this:但在所有帖子列表等其他页面中,我想要这样的东西:

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

what should i do?我该怎么办?

One way to do this would be to create a custom endpoint.一种方法是创建自定义端点。 This way you can write your own functions and export their results via the REST API.通过这种方式,您可以编写自己的函数并通过 REST API 导出其结果。 In fact the REST documentation has a sample that does something like what you're asking.事实上,REST 文档有一个示例,可以执行您所要求的操作。 See here: https://developer.wordpress.org/rest-api/extending-the-rest-api/adding-custom-endpoints/ and just modify their sample to remove the author query logic and just return the list of titles!请参阅此处: https : //developer.wordpress.org/rest-api/extending-the-rest-api/adding-custom-endpoints/并修改他们的示例以删除作者查询逻辑并返回标题列表!

Now Wordpress Rest Api is updated and you can get specific data as you required.现在 Wordpress Rest Api 已更新,您可以根据需要获取特定数据。 You need to send that field in _fields[] query For example: http://mywebsite.com//wp-json/wp/v2/posts?_fields[]=id&_fields=title您需要在 _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