【问题标题】:WordPress REST API - more than 10 postsWordPress REST API - 超过 10 个帖子
【发布时间】:2018-11-02 21:11:04
【问题描述】:

我搜索了这个问题并尝试了几种解决方案,但都没有成功。

我的主要路线在这里: https://cnperformance.wpengine.com/wp-json/wp/v2/products?_embed

我安装了“WP REST API 过滤器参数”插件来恢复当 REST API 移至 WordPress 核心时删除的过滤器。

我试过了: https://cnperformance.wpengine.com/wp-json/wp/v2/products?_embed&?filter[per_page]=-1

https://cnperformance.wpengine.com/wp-json/wp/v2/products?_embed&?filter[posts_per_page]=-1

我也在functions.php中试过这个

add_filter( 'rest_endpoints', function( $endpoints ){
    if ( ! isset( $endpoints['/wp/v2/products'] ) ) {
        return $endpoints;
    }
    unset( $endpoints['/wp/v2/products'][0]['args']['per_page']['maximum'] );
    return $endpoints;
});

参考这里:https://github.com/WP-API/WP-API/issues/2316

我已将posts_per_page 的值设置为100,-1,没有任何区别。我还尝试只添加参数 '&posts_per_page=-1 而不使用过滤器查询,但这也不起作用。非常感谢任何帮助或见解!

【问题讨论】:

  • 也许试试这个https://cnperformance.wpengine.com/wp-json/wp/v2/products?_embed&per_page=100
  • 我敢打赌,钱是行不通的,但它确实奏效了!本可以发誓我已经尝试过,但一定是在格式上关闭了。谢谢!!!
  • 请在此处查看我对这个问题的回答:stackoverflow.com/a/54382455/1211184
  • @kurtg 你能把我的答案标记为正确的还是有什么不清楚的地方?

标签: wordpress custom-post-type wordpress-rest-api wp-api


【解决方案1】:

是的,您一次可以获取多于默认的 10 个帖子。

只需将per_page 参数添加到您的请求中即可。

示例:https://cnperformance.wpengine.com/wp-json/wp/v2/products/?per_page=100

100 是当前的最大限制!

更多信息:https://developer.wordpress.org/rest-api/using-the-rest-api/pagination/


示例如何在一次加载超过 100 个项目

在您的第一次请求后带有 for 循环和总页数的信息:

https://github.com/AndreKelling/mapple/blob/master/public/js/mapple-public.js#L46

【讨论】:

  • 这就像一个魅力。但是您知道是否要一次加载 100 多个帖子?
  • @KushalJayswal 嗨,我编辑了我的帖子,并举例说明了如何借助 for 循环在总页数的帮助下做到这一点。
  • 感谢@AndréKelling - 知道这如何应用于 WP Rest API 插件而不是 Mapple 吗?
猜你喜欢
  • 2018-04-05
  • 2020-11-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-25
  • 2017-10-12
  • 1970-01-01
  • 2017-04-21
相关资源
最近更新 更多