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>')
});
});
}
});