admin管理员组文章数量:1432343
I want to get all text posts of a Tumblr blog with jQuery getJson using Tumblr's API.
I tried using the following code, but I just got 20 posts:
function loadPosts () {
var key = "api_key=xBcVPLfdDKpH0GjMCd1whW7rPoYkzLgZD3ZwpzndISFI4huSpA"
var api = ".tumblr/"
var post_amount
$.getJSON(api + "info?" + key,function(data) {
post_amount = data.response.blog.posts
$.getJSON(api + "posts/text?&filter=text&limit=" + post_amount + "&" + key,function(data) {
$.each(data.response.posts, function(i, item) {
var content = item.body
$("#Posts ul").append('<li>' + content + '</li>')
});
})
})
}
Here is a good sample Tumblr blog for testing:
/
I want to get all text posts of a Tumblr blog with jQuery getJson using Tumblr's API.
I tried using the following code, but I just got 20 posts:
function loadPosts () {
var key = "api_key=xBcVPLfdDKpH0GjMCd1whW7rPoYkzLgZD3ZwpzndISFI4huSpA"
var api = "https://api.tumblr./v2/blog/only-text-posts.tumblr./"
var post_amount
$.getJSON(api + "info?" + key,function(data) {
post_amount = data.response.blog.posts
$.getJSON(api + "posts/text?&filter=text&limit=" + post_amount + "&" + key,function(data) {
$.each(data.response.posts, function(i, item) {
var content = item.body
$("#Posts ul").append('<li>' + content + '</li>')
});
})
})
}
Here is a good sample Tumblr blog for testing:
http://only-text-posts.tumblr./
Share Improve this question edited Jul 26, 2014 at 18:10 Adi Inbar 12.3k13 gold badges59 silver badges70 bronze badges asked Jul 22, 2014 at 21:36 Alice ChanAlice Chan 2,9543 gold badges18 silver badges16 bronze badges 3- 2 Please add your code and read stackoverflow./help/how-to-ask – jgillich Commented Jul 22, 2014 at 21:39
- 1 @jgillich Sorry, This is my first question on StackOverflow. I'm now all clear about the rules, Thank you so much. – Alice Chan Commented Jul 26, 2014 at 17:38
- Just wanted to say, if you're doing this in a theme you could just exclude the other post types that you don't want to display/output (other than text posts) in the theme itself. I'm not sure what your end game is though.. – jonathanbell Commented Jan 16, 2016 at 6:34
2 Answers
Reset to default 4According to the documentation, only up to 20 posts are returned. You can specify an offset with the offset
parameter, and retrieve all posts with several calls:
function loadPosts () {
var key = "api_key=your_key";
var api = "https://api.tumblr./v2/blog/only-text-posts.tumblr./";
var retrieve_more = function (offset) {
$.getJSON(api + "posts/text?callback=?&filter=text&limit=20&offset=" + offset + "&" + key,function(data) {
$.each(data.response.posts, function(i, item) {
var content = item.body;
$("#Posts ul").append('<li>' + content + '</li>')
});
if (data.response.posts.length == 20) {
retrieve_more(offset + 20);
}
});
};
retrieve_more(0);
}
loadPosts();
fiddle
As from the Tumblr Api documentation there is a limit of 20 posts per request. You can perform multiple requests with an increasing offset.
var max_posts_per_page = 20;
$.getJSON(api + "info?" + key,function(data) {
post_amount = data.response.blog.posts;
for (var offset = 0; offset < post_amount; offset += max_posts_per_page) {
$.getJSON(api + "posts/text?&filter=text&limit=" + max_posts_per_page + "&offset=" + offset + "&" + key,function(data) {
$.each(data.response.posts, function(i, item) {
var content = item.body
$("#Posts ul").append('<li>' + content + '</li>')
});
});
}
});
本文标签: javascriptGet all posts from a Tumblr blog using jQuery getJsonStack Overflow
版权声明:本文标题:javascript - Get all posts from a Tumblr blog using jQuery getJson - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745593112a2665314.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论