wordpress - WP-API : max num pages

标签 wordpress wp-api

$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 ?如果我只是填写 currentPage1 ,没关系(因为假设它是第 1 页)。但是我怎么知道是否有第 2 页、第 3 页?

你有什么主意吗?非常感谢,任何帮助:)

最佳答案

WP_Query 在响应 header 中为您提供所需的内容。

您正在寻找的条款是 X-WP-TotalX-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/

相关文章:

wordpress - WordPress Rest api 中禁止获取草稿

wordpress - 如何在 Wordpress API v2 中过滤特定日期后修改的帖子

javascript - 如何在不传递用户名和传递url参数的情况下获取 token ?

javascript - 在 WooCommerce 结账时运行关于账单国家更改的 javascript 代码

php - Wordpress 子主题 style.css 已停止工作

wordpress - Amazon EC2 - PHP GD 图像库

html - 链接到页面的特定部分

php - WordPress 不断向短代码和内容添加自动段落标签

javascript - WPAPI : Parent Page not shown in WP UI for API created pages

wordpress - 无需身份验证即可使用 WP-REST 获取帖子