How do I code a button to link to random pages on my site?

So my website is split into four sections - news, comedy, recipes and funny. Each section has a multitude of pages underneath it.

Each section is represented by a large, clickable button on my home page.

I want to make it so that when each button is clicked, it takes the user to a random page within that section.

So for example, if they click the laughing face, they are taken to a random page within the ‘funny’ subset.

I’ve tried a bundle of random copy-paste Javascript I’ve found online, but nothing seems to be working.

Any ideas?

Hi Jack,

if this is a dynamic page, I would try querying a random item from the collection backing it. Say you query a single page from a category and skip a random number of pages:

wixData.query('pages')
  .eq('category', expectedCatogry)
  .skip(Math.random() * numberOfPages)
  .limit(1)
  .find()
  .then(result -> console.log(result.items[0].link-to-page))

You can get number of pages by using a count query:

wixData.query('pages')
  .eq('category', expectedCategory)
  .count()
  .then(result -> console.log(result))

I hope this puts you on the right track. Let me know how it went!

Hello
I was wondering how to have a rendom button for a blog web page. I would like by the button visitors can jump from one post to another rendomly. And have all the posts on one page together.
Thank you in advence