Answers:
WP Rest API 从WP_Query 发送总数count(found_posts)属性。在名为的标头中X-WP-Total
。
FOR POST:您可以调用REST API的post端点
http://demo.wp-api.org/wp-json/wp/v2/posts
帖子数的值在标头中返回为X-WP-Total
。以下是来自托管演示的示例响应
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
注意:
您还可以将获取的每页帖子数限制为1,这样就不会仅仅为了获得帖子数而获取所有的wordpress帖子
http://demo.wp-api.org/wp-json/wp/v2/posts?per_page=1
您所要做的就是向类别端点发出GET请求:
http://demo.wp-api.org/wp-json/wp/v2/categories
这将返回所有类别,并且类别的总数也可以在X-WP-Total
标题中找到。
尝试回答部分问题。标头中返回的wp rest api v2的计数。您可以像这样获得价值:
headers('X-WP-Total')
每页的帖子数:
wp-json/wp/v2/categories?page=1&per_page=5