Skip to content
● Meet Grav Guard: keep every Grav site you run secure and up to date, all from one place. Free for up to 3 sites. Read more →

Pulling two random images from folder

Started by Reinier 9 years ago · 2 replies · 1253 views
9 years ago

I hope my question isn't to trivial...
how to accomplish the following:

I want to pull several random images from a folder but would like to prevent doubles.

Im using this bit of code:

{% set image = page.media.all|randomize|first %}
{% if image %}
{{ image.cropResize(400,400).html('','','align-'~page.header.image_align) }}
{% endif %}


I use it twice, two images are pulled.... but allthough random, also double.
Is there a simple way for this? something like after randomize pull the |first&last from the generated aray? (if its an array being created that is)

9 years ago

You almost got it!
This should work

TWIG
{% set image = page.media.all|randomize %}
{% if image|first %}
{{ image|first.cropResize(400,400).html(’’,’’,‘align-’~page.header.image_align) }}
{{ image|last.cropResize(400,400).html(’’,’’,‘align-’~page.header.image_align) }}
{% endif %}
👍 1
9 years ago

First of, thanks for the reply; its greatly appreciated Paul.

At first no go, error images showed.
clearing the cache made your code work.

last edited 04/06/18 by Reinier

Suggested topics

Topic Participants Replies Views Activity
Themes & Styling · by themetman, 6 days ago
1 98 2 days ago
Themes & Styling · by AH, 3 weeks ago
1 219 3 days ago
Themes & Styling · by Bruno, 4 years ago
6 1382 3 days ago
Themes & Styling · by TomW, 3 weeks ago
2 223 5 days ago
Themes & Styling · by FrViPofm, 2 months ago
4 684 1 month ago