Search code examples
twiggrav

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.


Solution

  • 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.