Search code examples
jquerygetjsonflickreach

how to make a specific loop on $.getJSON and $.each jquery function?


I have this code to retrieve Flickr images. However, I just want to retrieve only let say 4 images, how to edit this code?

Thanks!

jQuery(document).ready(function($){
                $.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?ids=<?php print $flickrid; ?>&lang=en-us&format=json&jsoncallback=?", function(data){
                      $.each(data.items, function(index, item){
                            $("<img/>").attr("src", item.media.m).appendTo("#flickr")
                              .wrap("<li><a href='" + item.link + "'></a></li>");
                      });
                    });
            });

Solution

  • The lazy way:

    $.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?ids=<?php print $flickrid; ?>&lang=en-us&format=json&jsoncallback=?", function(data){
        $.each(data.items, function(index, item){
            if (index<5) {
                $("<img/>").attr("src", item.media.m).appendTo("#flickr")
                           .wrap("<li><a href='" + item.link + "'></a></li>");
                });
            }
        });
    });