For a blog I’m working on at the moment I want to implement a Random Post button in the header. I tried using something like below to get a random post:
$page->children()->visible()->shuffle()->first()
It works fine, until I turn on Kirby’s cache. I guess this is probably expected behaviour, but now the output of the above is cached, and the user no longer sees a random page, but the same one over and over again.
Maybe I’m looking at this all wrong, but is there a way I can achieve this without disabling the cache?
Any help is greatly appreciated, and apologies upfront if this is a noob question
Simon
I don’t think the param idea is going to work as the resulting HTML of the button will be cached again, no matter if using a param or not.
You could use a route like /random and link to that in the header. The route would then select the random post and redirect to it. This way you can cache all pages and the result would still be random.
I couldn’t get the parameter solutions working, unfortunately, but I’ve made some progress using the route solution provided by @lukasbestle and @texnixe.
It’s all working except for one thing: The site is 404’ing when used in conjunction with the routes in the guide here to omit the home/ folder from URLs. Any ideas why?
A weird thing though, it works perfectly on my local machine, but is completely broken on the live server. Do you have any ideas why that would happen? I can’t work it out at all.
Edit: the shuffle button at the top right of the page
Not 100% sure about this one and I can’t test it because I’m not at home but you can try adding the /random Route to a cache.ignore rule in the config file.
Something like c::set('cache.ignore', array( 'random'));