Random page image from folder?

I’m trying to get a random image from a folder and use that to stick at the top of listing pages. I found a snippet in an old thread on the forum which I understand should work pretty well but it’s not doing what I thought.

The snippet I’m using is:

{{ (readDir "/public/images/covers" | shuffle | first 1) }}

This outputs on the page:

[0xc42042a8f0]

Which I guess is a FileInfo object. So I think, “I should just be able to grab the name from that”, like this:

({{ (readDir "/public/images/covers" | shuffle | first 1) }}).Name

But that gives me an error about the Name property not being available. Has anyone got any guidance on this?

Thanks :smiley:

Well, I cracked it I think. Typical. I reread the error message and saw it was referring to an array of FileInfo objects. I think I misunderstood what the “first 1” bit did. Anyway, I did this (in case people want to know)

(index (readDir "/public/images/covers" | shuffle | first 1) 0).Name

The syntax looks a little messy if anyone has any cleanup ideas but it works well enough :slight_smile:

2 Likes