首先,我搜索了几个问题和文档,但似乎是WordPress的v2
许多老问题不再有效。我想做的是从Postman中的一个类别中获取所有帖子或单个帖子,而不是常见的返回10篇帖子,而无需修改函数中的API。php。
tldr我从引用REST API Handbook 并查看schema 我看到了categories
我可以使用以下方法返回10个最新类别:
http://foobar.com/wp-json/wp/v2/posts/categories_name=hello
引用arguments 我明白了per_page
所以我试着:http://foobar.com/wp-json/wp/v2/posts/categories_name=hello?per_page=1
它从类别中返回10个帖子hello
所以我修改并尝试:http://foobar.com/wp-json/wp/v2/posts/categories_name=hello&per_page=1
我得到一个返回的错误:{“code”:“rest\\u invalid\\u param”,“message”:“无效参数:per\\u page”,“data”:{“status”:400,“params”:{“per\\u page”:“per\\u page不是整型。”}}}}}
通过Google I搜索see How to retrieve a list of categories/ tag in Wordpress REST API 但答案是基于v1
.
尝试Wordpress API JSON return limit 我使用:
http://foobar.com/wp-json/wp/v2/posts/?per_page=1
我得到了一个单数帖子,所以我修改了我的尝试:http://foobar.com/wp-json/wp/v2/posts/?per_page=1$categories_name=hello
它忽略类别类型并返回最新的帖子。阅读Get more than 10 posts in a specific category with the WordPress API 使用以下命令后,我提取了类别(4)的ID:http://foobar.com/wp-json/wp/v2/categories
然后编码:http://foobar.com/wp-json/wp/v2/posts/?categories=4&per_page=1
我得到:{“code”:“rest\\u invalid\\u param”,“message”:“无效参数:per\\u page”,“data”:{“status”:400,“params”:{“per\\u page”:“per\\u page不是整型。”}}}}}
我想我可以用-1
类似于主题的开发,但我有一个错误。
我读到的其他参考文献有:
- WP REST API - Retrieve content from page
- WP REST API CategorySince filter has been removed, how to get posts by category slug with same schema as v2/posts? 阅读后Search post by categories Wordpress WP-API
post
而不是任何category
. 如果我使用/wp-json/wp/v2/posts?per_page=20
, 也问题
调用站点的WP API时,如何控制per_page
返回类别是1个帖子还是所有帖子?