简体   繁体   English

如何指定 Wordpress API v2 的字段

[英]How To Specify the fields to Wordpress API v2

I'm using the WordPress REST API in my project and sending a GET request to:我在我的项目中使用 WordPress REST API 并将 GET 请求发送到:

http://myblog/wp-json/wp/v2/posts  

and It's working quite alright but I want to specify the fields though I don't know how.它工作得很好,但我想指定字段,尽管我不知道如何。 I have looked at the documentation and still don't know how to go about it.我已经查看了文档,但仍然不知道如何去做。 For example, using the public API:例如,使用公共 API:

https://public-api.wordpress.com/rest/v1.1/sites/www.mysite.com/posts?number=100&fields=title,excerpt,featured_image 

returns only the specified fields.只返回指定的字段。 How do I this with the v2 API?我如何使用 v2 API 做到这一点?

以下是使用 REST API v2 访问标题和摘录列表的方法:

https://www.example.com/wp-json/wp/v2/posts?_fields[]=title&_fields[]=excerpt&per_page=100&offset=100

https://developer.wordpress.org/rest-api/extending-the-rest-api/modifying-responses/ https://developer.wordpress.org/rest-api/extending-the-rest-api/modifying-responses/

As it states there, the REST API v2 returns a certain set of default fields, and if you want different ones, then you have to implement this as described in that document.正如它所说的那样,REST API v2 返回一组特定的默认字段,如果您想要不同的字段,那么您必须按照该文档中的描述来实现它。

The easy solution to this issue would be to use the ACF to REST API or an equivalent plugin that can extend the REST API for you.此问题的简单解决方案是使用ACF to REST API或可以为您扩展 REST API 的等效插件。 I have used this plugin on many sites successfully.我已经在许多网站上成功使用了这个插件。

If this is not possible then you will need to modify the response as has been outlined by other answers.如果这是不可能的,那么您将需要修改其他答案中概述的响应。 You can read more about that here .您可以在此处阅读更多相关信息。

您可以使用 ?_fields[]=title&_fields[]=excerpt

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

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