$opts = array(); // any arguments
$query = WP_Query($opts);
通常,我们可以得到 max_num_pages 通过使用
$totalPages = $query->max_num_pages;
但是,我使用这个插件发出 json 请求 http://wp-api.org/ .当我尝试使用 API 检索帖子时,响应只是 json 对象/帖子数组。问题是,
$query->max_num_pages;
无法访问。我需要那个 var 来进行分页请求。例如,我想提出一个请求:
$.ajax({
url: 'wp-json/posts?page=' + currentPage + '&filter[posts_per_page]=' + perPage + '&filter[cat]=' + category
});
如何填写
currentPage
var 如果我们不知道 max_num_pages ?如果我只是填写 currentPage
与 1
,没关系(因为假设它是第 1 页)。但是我怎么知道是否有第 2 页、第 3 页?你有什么主意吗?非常感谢,任何帮助:)
最佳答案
WP_Query 在响应 header 中为您提供所需的内容。
您正在寻找的条款是 X-WP-Total
和 X-WP-TotalPages
.
在 jQuery 中,您可以像这样获得 AJAX 响应 header :
$.ajax({
url: myURL,
success: function(data, textStatus, request){
console.log("This is the header you're looking for",request.getResponseHeader('X-WP-Total'));
}
});
Note I didn't have the opportunity to test this properly as I'm not by my dev machine
关于wordpress - WP-API : max num pages,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28940100/