Custom code to view different image on slideshow/gallery every time the website is opened or reloaded

You could create a custom dataset (CMS) with the images you want, beign an image per line, description and title for each image.

Them, put the gallery on the site and set a code to randomly grab an image from your collection.

wixData.query("myCollection")
  .find()
  .then( (results) => {
    let randomNumber =  Math.floor((Math.random() * results.items.length)); 
    let randomItem = results.items[randomNumber];

Remember to change “myCollection” to your collection name.

Then, you would also need a code to atribute the randomItem to your gallery, you can check this: https://www.youtube.com/watch?v=rKwx5jnf0Wc

Or, click here.

There are two approaches: to fill the gallery with the random generated number as a index for you array (collection) or using the first random generated number and adding 1 from there. The first one has the probblem that the numbers can repeat, so you should solve this.

1 Like