Random Post Button

Hey all! I run my personal blog over at https://www.ntnbr.com.

I’d like to have a link (say: ntnbr.com/random) that automatically redirects the user to a random post of mine. Thankfully, all of my posts have the format ntnbr.com/NUMBER (where NUMBER is a positive integer). I have 39 posts published so far.

Is there some script or functionality that I can use to tell the server “hey, choose a random number from 1-39 and then append it after https://ntnbr.com/ if the user visits ntnbr.com/random” ?

Keep in mind that I’m using Ghost(Pro). I would appreciate any help!

I don’t think there’s a handlebars way to do this, but you could definitely have some code on the /random page that runs a little javascript and sends the user to a random page. Or you could run some javascript to pull the random page from the API and render it, but just doing a redirect is going to be super easy.

Figured it out! The redirect was super easy – only took six lines of javascript.

It now works if you visit /random.