当前位置:K88软件开发文章中心编程全书编程全书02 → 文章内容

wp rest API获取文章页数及文章总数

减小字体 增大字体 作者:佚名  来源:网上搜集  发布时间:2019-1-4 9:21:21

-->

今天在处理文章数据加载时候出现无法判断最大页码,于是google到的一些答案。

The WP Rest API sends the total count(found_posts) property from WP_Query. in a header called X-WP-Total.

FOR POSTS:?you can make a call to posts endpoint of the REST API

http://demo.wp-api.org/wp-json/wp/v2/posts

The value for posts count is returned in the header as X-WP-Total. Below is a sample response from the hosted demo

Access-Control-Allow-Headers:Authorization, Content-Type
Access-Control-Expose-Headers:X-WP-Total, X-WP-TotalPages
Allow:GET
Cache-Control:max-age=300, must-revalidate
Connection:keep-alive
Content-Encoding:gzip
Content-Type:application/json; charset=UTF-8
Date:Wed, 28 Dec 2016 12:48:50 GMT
Last-Modified:Wed, 28 Dec 2016 12:48:50 GMT
Link:<https://demo.wp-api.org/wp-json/wp/v2/posts?page=2>; rel=”next”
Server:nginx/1.4.6 (Ubuntu)
Transfer-Encoding:chunked
Vary:Cookie
Via:1.1 dfa2cbb51ec90b28f03125592b887c7d.cloudfront.net (CloudFront)
X-Amz-Cf-Id:ri4C3e-AdixwqGv_wYNdGRq9ChsIroy1Waxe2GqkiTqbk4CpiSIQfw==
X-Batcache:MISS
X-Cache:Miss from cloudfront
X-Content-Type-Options:nosniff
X-EC2-Instance-Id:i-198c7e94
X-Powered-By:PHP/7.0.11-1+deb.sury.org~trusty+1
X-Robots-Tag:noindex
X-WP-Total:71
X-WP-TotalPages:8

这里具体使用实例:

参考文档:

wordpress官方文档(英语能力不行,当时没看懂~~): https://developer.wordpress.org/rest-api/using-the-rest-api/pagination/

stackoverflow: https://stackoverflow.com/questions/48503413/wordpress-rest-api-get-total-post-count


wp rest API获取文章页数及文章总数