本文档介绍了 WordPress REST API 中的分页机制,用于处理大量内容的分批请求。默认情况下,API 端点限制每页返回的项目数,类似于 WordPress 站点在归档视图中默认每页显示 10 篇文章。
/wp/v2/posts?page=2
/wp/v2/posts?per_page=1
/wp/v2/posts?offset=6
/wp/v2/posts?per_page=5&page=4WordPress sites can have a lot of content—far more than you’d want to pull down in a single request. The API endpoints default to providing a limited number of items per request, the same way that a WordPress site will default to 10 posts per page in archive views.
Any API response which contains multiple resources supports several common query parameters to handle paging through the response data:
?page=: specify the page of results to return.
/wp/v2/posts?page=2 is the second page of posts results/wp/v2/posts, then /wp/v2/posts?page=2, and so on, you may access every available post through the API, one page at a time.?per_page=: specify the number of records to return in one request, specified as an integer from 1 to 100.
/wp/v2/posts?per_page=1 will return only the first post in the collection?offset=: specify an arbitrary offset at which to start retrieving posts
/wp/v2/posts?offset=6 will use the default number of posts per page, but start at the 6th post in the collection?per_page=5&page=4 is equivalent to ?per_page=5&offset=15per_page is capped at 100 records. If you wish to retrieve more than 100 records, for example to build a client-side list of all available categories, you may make multiple API requests and combine the results within your application.
To determine how many pages of data are available, the API returns two header fields with every paginated response:
X-WP-Total: the total number of records in the collectionX-WP-TotalPages: the total number of pages encompassing all available recordsBy inspecting these header fields you can determine how much more data is available within the API.
In addition to the pagination query parameters detailed above, several other parameters control the order of the returned results:
?order=: control whether results are returned in ascending or descending order
?order=asc (for ascending order) and ?order=desc (for descending order).?orderby=: control the field by which the collection is sorted
orderby will vary depending on the queried resource; for the /wp/v2/posts collection, the valid values are “date,” “relevance,” “id,” “include,” “title,” and “slug”orderby=date