Aabha Pandey
Aabha Pandey

Reputation: 905

I have a gallery of images in Grav/Twig and want a few of them to be displayed on homepage but randomly

Currently using page.find('/folder_name').media.images to extract images and setting limit in for loop. But no way to randomize them. And setting limit in for loop is I believe not a good way of doing it. Could anybody help?

Here I am asking for gallery. The suggested method works on articles but not on media collection.

Upvotes: 1

Views: 504

Answers (1)

Aabha Pandey
Aabha Pandey

Reputation: 905

Got success. I used randomize function as follows to random my image collection with slice to limit it to only 50 images.

image_album = page.find('/folder_name').media.images|randomize()|slice(0,50)

This works for me and gave desired result.

Upvotes: 2

Related Questions