Instafeed js add wrapper around every 4 image

1.2k Views Asked by At

I am using instafeed js to call photos from instagram. Is there a way to wrap every 4 images inside a div? is this even possible? Here is my code:

  jQuery(window).load(function(){

      var userFeed = new Instafeed({
        get: 'user',
        userId: 1509441625,
        accessToken: '1509441625.5b9e1e6.c20b3eb91b15404ab30084283ab3a9c9',
        limit : 4,             
        resolution:'standard_resolution',
        template: '<a target="_Blank" href="{{link}}"><img src="{{image}}" /></a> ',

      });
      userFeed.run();

    });

I reached out to the developer of instafeed and he gave me a untested chunck of code that im trying to debug:

      var count = 0;
      var userFeed = new Instafeed({
        get: 'user',
        userId: 1509441625,
        accessToken: '1509441625.5b9e1e6.c20b3eb91b15404ab30084283ab3a9c9',
        limit : 4,             
        resolution:'standard_resolution',
        filter: function(image) {
            count += 1;
            if (count % 4 === 0) {
                image.customTagOpen = '<div>';
                image.customTagClose = '</div>';
            } else {
                image.customTagOpen = '';
                image.customTagClose = '';
            }
            return true;
        },
        template: '{{model.customTagOpen}}<a target="_Blank" href="{{link}}"><img src="{{image}} /></a>{{model.customTagClose}}';

      });
      userFeed.run();

    });

But I get a error : missing "}" after property listing. any advice?

3

There are 3 best solutions below

1
On BEST ANSWER

There is an incorrectly placed semicolon at the end of the template option line.

Try changing that semicolon ; to a comma ,

template: '{{model.customTagOpen}}<a target="_Blank" href="{{link}}"><img src="{{image}} /></a>{{model.customTagClose}}',
0
On

For div row every 4 count use this:

  filter: function(image) {
    count += 1;
    if (count == 1 || (count - 1) % 4 == 0 ) {
      image.customTagOpen = '<div class="row">';
      image.customTagClose = '';
    } else if (count % 4 == 0) {
      image.customTagOpen = '';
      image.customTagClose = '</div>';
    } else {
      image.customTagOpen = '';
      image.customTagClose = '';
    }
    return true;
  },

or use

  filter: function(image) {
    if (count == 0 || count % 4 == 0 ) {
      image.customTagOpen = '<div class="row">';
      image.customTagClose = '';
    } else if ((count + 1) % 4 == 0) {
      image.customTagOpen = '';
      image.customTagClose = '</div>';
    } else {
      image.customTagOpen = '';
      image.customTagClose = '';
    }
    count += 1;
    return true;
  },
0
On

Addition to Steven's answer you can use this fix:

You can use this fix:

if (count % 4 === 0 || count === 0) {
    image.customTagOpen = '<div>';
    image.customTagClose = '';
} else if (count + 1 % 4 === 0) {
    image.customTagOpen = '';
    image.customTagClose = '</div>';
} else {
    image.customTagOpen = '';
    image.customTagClose = '';
}

Cheers,