Randomise the display of testimonials using repeater and content manager

Hi Community,

I have a collection of customer testimonials that I would like to display on my website. I only want to present a few testimonials at a time / per page visit but I would like to randomise the testimonials presented so that a visitor is presented with different testimonials each time they re-visit / refresh the page (just trying to keep the content fresh).

Is this possible? And if so how would I achieve this?

I wondered if it would be possible to use a repeater linked to a collection of testimonials stored in the content manager but I’m not sure how I could randomise the eventual presentation of entries.

Could anybody offer any advice or links to further information?

Many thanks

I would approach this by using Velo.

import wixData from 'wix-data';

$w.onReady(function () {
    $w("#datasetID").onReady(() => {
        wixData.query("CollectionID")
            .limit(5)
            .find()
            .then((result) => {
                const shuffledArray = shuffleArray(result.items);
                $w('#repeaterID').data = shuffledArray;
            })
            .catch((err) => {
                let errorMsg = err;
            });
    });
});


function getRandomIndex(min, max) {
    return Math.round(Math.random() * (max - min) + min);
}


function shuffleArray(dataArray) {


    for (let i = dataArray.length - 1; i > 0; i--) {
        let index = getRandomIndex(0, i);
        const temp = dataArray[i];
        dataArray[i] = dataArray[index];
        dataArray[index] = temp;
    }


    return dataArray;
}

Copy and paste the above code, replacing what you have already added.

Then change the following:
limit(5) - change the number to however many results you want to return
datasetID - change to your dataset ID
CollectionID - change to the ID of the collection
repeaterID - change to the ID of the repeater

I have tested it and it does work. If you need any help with it, let me know :facepunch: