Kevin Whitaker
Kevin Whitaker

Reputation: 457

Getting a specific number of JSON items with jQuery from tumblr feed

I'm trying to populate a page with X entries from my tumblr feed, and I'm wondering how I can only pull that X number from the JSON object it returns.

Here's my code, pulled from another Stack Overflow post and modified:

//Tumblr retrieval
$.getJSON("http://tumblr-address/api/read/json?callback=?", 
  function(data) {
    $.each(data.posts, function(i,posts){
      var title = this["regular-title"]; 
      var type = this.type; 
      var date = this.date; 
      var url = this["url-with-slug"];

      $('#sideRail ol').prepend('<li><p><a href=' +url +'>' + title + '</a></p><p>' + date + '</p></li>'); 
    });
 });

I've tried using a while loop with a counter, but it just repeats everything X times before moving on to the next item in the list.

Thanks for any help.

Upvotes: 2

Views: 3391

Answers (3)

Taylor Suchan
Taylor Suchan

Reputation: 31

old post but updates info cant hurt... yes the old api allowed the num= parameter to specify a l8imit to returned items, the new Api version 2 uses 'limit=' instead. but defaults to 20 if left out.

Upvotes: 1

Ateş G&#246;ral
Ateş G&#246;ral

Reputation: 140050

You can use the num query parameter:

$.getJSON("http://tumblr-address/api/read/json?num=20", ...

And I don't think you need to have a blank callback parameter. You're not doing JSONP.

Upvotes: 10

John Millikin
John Millikin

Reputation: 200766

Use the Array.slice method on the post array. For example, to retrieve 10 items:

$.getJSON("http://tumblr-address/api/read/json?callback=?", 
  function(data) {
    $.each(data.posts.slice(0, 10), function(i,posts){
      // ...

Upvotes: 2

Related Questions