Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

Themes & Styling

Pulling two random images from folder

Started by Reinier 8 years ago · 2 replies · 775 views
8 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)

8 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
8 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 Pedro M, 2 months ago
4 195 2 months ago
Themes & Styling · by Ian, 2 months ago
3 91 2 months ago
Themes & Styling · by Norbert, 2 years ago
11 451 3 months ago
Themes & Styling · by Lukáš Findeis, 3 months ago
0 45 3 months ago
Themes & Styling · by Sebadamus, 4 months ago
5 125 3 months ago