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

Get random image from certain page

Started by Muut Archive 9 years ago · 2 replies · 778 views
9 years ago

Hi everyone,
I have a certain kind of page (company). In the admin on the company page, the user can upload a logo for every company. Now I would like to show a random company logo on the homepage. How can I do such a thing?

In the blueprint I added the following line to determine the location of the logos: destination: 'theme@:/assets/logos'

How do I accomplish this? Thanks a lot for your help in advance!

9 years ago

What i would do, is put all the logos into a page folder such as user/pages/logos. You don't need a .md file, just the logos themselves. That way you can get all the logos as an array via:

TWIG
{% set logos = page.find('/logos').media.images %}

Then simply pick a random one from that array:

TWIG
{% set random_logo = random(logos) %}

Then you can just output that as an HTML tag:

TWIG
{{ random_logo.html }}

or a URL:

TWIG
<a href="{{ home_url }}"><img src="{{ random_logo.url }}" /></a>
👍 1

Suggested topics

Topic Participants Replies Views Activity
Archive · by Deleted User, 9 years ago
0 1346 9 years ago
Archive · by Muut Archive, 9 years ago
2 933 9 years ago
Archive · by Muut Archive, 9 years ago
2 4060 9 years ago
Archive · by Muut Archive, 9 years ago
1 2945 9 years ago
Archive · by Muut Archive, 9 years ago
3 1117 9 years ago