Load an entire random page

After playing around with Kirby for a little while, I have an idea for a little project I want to try build.

In short, I want to load an entire new random page every time you refresh (think like Stumbleupon, but it loads your own pages).

Is there a way I could do this? I need to somehow combine routing with shuffling :slight_smile:

Thanks!

Try shuffe and first:

$pages->shuffle()->first()

https://getkirby.com/docs/cheatsheet/pages/shuffle
https://getkirby.com/docs/cheatsheet/pages/first

or create a collection of pages you want and shuffle&first it then.

maybe a router would be a good idea. If somebody calls the home page a random page will be picket… with go().

go($pages->shuffle()->first()-uri())

(uri)

or…

give the home page a β€œhome” template and put the go code there. So every time somebody calls the home page (with the home template) a random page will be displayed and the subpages are accessible like always because they use other templates.

The question here is which pages you want to pick randomly and where/how these pages should be displayed. Of course, you could use a random route, which then picks any page randomly and shows it.

Awesome, thanks for the help @Svnt! I got it working how I wanted with the following (also thanks to @texnixe for one of your old forum replies :wink: )

c::set('routes', array(
  array(
    'pattern' => '/',
    'action'  => function() {
    	$page = page('posts')->children('visible')->shuffle()->first();

      	return site()->visit($page);
    }
  )
));

My pool of pages is only small at the moment, so the same page will often come up. Obviously this will decrease as more pages are added, but I’d still like to prevent it from happening.

Could I somehow exclude the current uri() from the shuffle?

You could save the old uri in a cookie before you return the page. And use not() no exclude the URI.

c::set('routes', array(
  array(
    'pattern' => '/',
    'action'  => function() {
      $page = page('posts')->children('visible')->not(cookie::get('oldUri'))->shuffle()->first();   
      cookie::set('oldURI', $page->uri());

        return site()->visit($page);
    }
  )
));