Random content

Random content is a great way to simulate activity on your site. Especially the homepage is a perfect place to show randomly selected articles from your blog, a different header image each time the page gets refreshed or a box with a random featured project from your portfolio.

There are multiple ways to use this technique to create more life on your site even if you don't update it very often and it gives you the chance to promote older content, which might already be burried deep down below newer stuff.

Fetching random pages

Kirby has a built-in method to sort pages randomly. You can use that for any set of pages. Here are some examples:

// will return the main pages of your site
// in random order

// will return all your projects from your portfolio
// in random order.

// will return all visible articles
// from your blog in random order

You can even get a defined set of pages by URI and shuffle that.

$pages->find('projects/project-a', 'blog/my-first-article')->shuffle();
// will return a set of the two pages in random order
// you can add as many URIs as you like.

Limit the result

Sorting a set of pages randomly will still return the entire set of pages of course. So if we only need one or just a few random pages for our homepage for example, we have to limit the result afterwards. There are two ways to do it:


$featured = $pages->find('projects')->children()->shuffle()->first();
// this will return the first random project
// and we can now get the title or any other data

<div class="featured-project">
  <h1><?= $featured->title() ?></h1>
  <h2><?= $featured->subtitle() ?></h2>
  <a href="<?= $featured->url() ?>">Read more…</a>

If we need two, three or more featured projects, we can do it like this:


$featured = $pages->find('projects')->children()->shuffle()->limit(3);
// this will return three random projects

<ul class="featured-projects">
  <?php foreach($featured as $project): ?>
    <h1><?= $project->title() ?></h1>
    <h2><?= $project->subtitle() ?></h2>
    <a href="<?= $project->url() ?>">Read more…</a>
  <?php endforeach ?>

So with those simple methods we can easily get any random content from our site.

Random files and images

Random content is cool, but random images are even more awesome. Think about random background or header images, shuffled galleries and stuff like that. There's so much you can do with it. The method to fetch random images is exactly the same as fetching random content.

// this will return a random set of images for the current page

// this will return only one random image for the current page

// this will return a random set of three images for the current page

Fetching random images from other pages

If you are building a template for the homepage and you need to get random images from any page of your site, you can do it like this:

$project = $pages->find('portfolio/project-a');
$images  = $project->images()->shuffle();
// this will return a random set of images from portfolio/project-a

You could even get a random image from a random page:

$randomPage  = $pages->find('portfolio')->children()->shuffle()->first();
// get a random page from your portfolio
$randomImage = $randomPage->images()->shuffle()->first();
// get a random image from that page