使用WordPress REST API嵌套第二个$.getJSON请求以检索类别名称/链接
Nesting a second $.getJSON request with the WordPress REST API to retrieve category names/links
我正在使用WordPress REST API来检索博客文章,这一切都很好。我有一个初始的$.getJSON
调用来检索相关的帖子,然后通过数据$.each
返回要呈现到页面的相关HTML块。
然而,我有点拘泥于如何实现每个帖子的类别列表。posts端点只列出类别ID,所以我需要以某种方式查询类别端点,传入ID,以构建类别链接。我可以在$.each
中嵌套一个$.getJSON
调用来查询该帖子的类别吗?正在为如何构建它而挣扎。
这就是我到目前为止所拥有的(只是提取了相关代码的片段):
var state = {
totalPages: null,
posts: [],
page: 1
}
getPostsByCategoryId: function getPostsByCategoryId(id, container) {
$.getJSON('/wp-json/wp/v2/posts?filter[cat]='+id+'&filter[orderby]=title&filter[order]=ASC&filter[posts_per_page]=4&page='+state.page+'', function(data, status, xhr) {
state.totalPages = xhr.getResponseHeader('X-WP-TotalPages');
$.each(data, function() {
var featuredImage = null;
var cats = this.categories;
var categories = [];
// // Loop over the categories
$.each(cats, function() {
$.getJSON('/wp-json/wp/v2/categories/13', function(data) {
var categoryItem = '<a class="category" href="' + data.link + '">' + data.name + '</a>';
categories.push(categoryItem);
});
});
var post = $('<div class="news"><div class="detail"><p class="postmeta"><a class="category" href="#">CATEGORIES</a></p><h3 class="title"><a href="' + this.link + '">' + this.title.rendered + '</a></h3><a href="' + this.link + '">View article</a></div></div>');
state.posts.push(post);
});
container.append(state.posts);
if (state.page == state.totalPages)
$('#loadmore').addClass('disabled');
state.page++;
});
return false;
}
谢谢:)
更新代码-无法读取未定义的属性
getPostsByCategoryId: function getPostsByCategoryId(id, container) {
$.getJSON('/wp-json/wp/v2/posts?filter[cat]='+id+'&filter[orderby]=title&filter[order]=ASC&filter[posts_per_page]=4&page='+state.page+'', function(data, status, xhr) {
state.totalPages = xhr.getResponseHeader('X-WP-TotalPages');
$.each(data, function() {
var featuredImage = null;
var cats = this.categories;
var categories = [];
var requests = [];
// // Loop over the categories
$.each(cats, function() {
var xhr = $.getJSON('/wp-json/wp/v2/categories/13', function(data) {
var categoryItem = '<a class="category" href="' + data.link + '">' + data.name + '</a>';
categories.push(categoryItem);
});
requests.push(xhr);
});
$.when.apply($, requests).then(function() {
var post = $('<div class="news"><div class="detail"><p class="postmeta"><a class="category" href="#">CATEGORIES</a></p><h3 class="title"><a href="' + this.link + '">' + this.title.rendered + '</a></h3><a href="' + this.link + '">View article</a></div></div>');
state.posts.push(post);
});
});
container.append(state.posts);
blogPostLoader.toggleLoadingIcon($('.loading i'));
if (state.page == state.totalPages)
$('#loadmore').addClass('disabled');
state.page++;
});
return false;
}
这不能在纯jQuery中完成。您需要为REST API编写自定义端点。在这个端点,您将返回每个帖子的类别。这是一项相当大的工作,有官方的WP教程如何将自定义端点添加到API:
http://v2.wp-api.org/extending/adding/
我认为这会有所帮助,如果你需要更多帮助,请告诉我。
相关文章:
- 使用JSP从服务器检索和显示图像
- 通过Magento的网络服务检索运费
- 如何通过php页面将数据库值检索到jquery自动完成框中
- 无法使用javascript检索SPList项
- 可以't使用Angular解析/检索JSON
- GoogleFeed正在检索图像
- 通过ajax将坐标传递到php服务器端,并在处理后检索到javascript
- 使用Scala Play Framework视图中的键检索映射值
- 从数据库中检索字段,而不模拟它们
- 如何在corona sdk中从CK编辑器中检索数据
- 如何使用Javascript客户端对象模型检索Sharepoint 2010列表项权限
- 在Node.js中上传和检索图像
- XML2JSON并检索数据
- 如何从php返回的JSON中检索值
- 如何在.js函数中检索来自其他模板的表单的目标值
- 使用ASP.NET将谷歌地图添加到项目中,并从数据库中检索位置
- 使用javascript进行JSON数据检索
- 检索MongoDB binData并显示为<img>src
- 为什么可以't我将检索到的文档分配给控制台中的变量
- 别名或以其他方式合并两个具有不同名称的相同对象原型