I have a single that has 5 banner images. I would like to load a different image each time the page loads. I want to do this with twig not JS, is this possible if so could somone point me in the right direction.
Thanks in advance
Craft CMS Stack Exchange is a question and answer site for administrators, end users, developers and designers for Craft CMS. It only takes a minute to sign up.
Sign up to join this communitySimply using 'RAND()'
in the order parameter does the trick.
{% for image in entry.banner.limit(1).order('RAND()') %}
{{ image.url }}
{% endfor %}
I wanted to see if the same thing could be accomplished using Craft's shuffle function. At first, I was looping through the image array and using an if statement to grab the first one:
{% for image in shuffle(entry.banner) %}
{% if loop.first %}
{{ image.url }}
{% endif %}
{% endfor %}
Then, I realized there was a less verbose option:
{% set image = shuffle(entry.banner)[0] %}
{{ image.url }}
Another way of getting a random image from image set in Craft 3
{{ shuffle(entry.bgImg.all())[0].getUrl('1900x1050') }}