使用WordPress REST API嵌套第二个$.getJSON请求以检索类别名称/链接

Nesting a second $.getJSON request with the WordPress REST API to retrieve category names/links

本文关键字:检索 别名 链接 请求 getJSON REST WordPress API 嵌套 第二个 使用      更新时间:2023-09-26

我正在使用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/

我认为这会有所帮助,如果你需要更多帮助,请告诉我。