Randomly Load an Item from a Custom Mod

I’ve been searching and searching, but can’t find the solution, or I’m not looking in the right place or using the right keywords.

Is it possible to have a custom module of say 50 items, just output 1 randomly? I’m not talking about the order. On page load, go to [module] and randomly display one item (which happens to be an image if that helps).

Any assistance would be greatly appreciated.

Thanks in advance.

The random parameter will work in this case along with a limit of 1.

For example:

{% component source: "Portfolio", layout: "List", random: "true", limit: "1", type: "module" %}

Documentation for more info:
Component Type: Module (Custom Modules)

When testing this on your site and refreshing the page, the random result is stored in a Liquid cache for around 10 seconds. So refreshing the page within 10 sec will return the same item (this is for performance reasons).

1 Like

Great, thank you. That is what I started with, but didn’t know about the 10-second refresh. So when testing, it was stuck on the same record every time, so I thought I was doing something wrong.

Thanks again!

Thank you Adam! Once again you have helped me in a situation where I was stuck scratching my head. I needed something similar to select a page title background image at random from a Custom Module and adding limit: “1” did the trick. I could not figure out why all of the Custom Modules were showing in the background image parameter :person_facepalming: Anyways, I know this is an old post but thank you!

1 Like