👀 Get a glimpse of Kirby 5 Learn more
Skip to content

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:

$pages->shuffle();
// will return the main pages of your site
// in random order

page('projects')->children()->shuffle();
// will return all your projects from your portfolio
// in random order.

page('blog')->children()->listed()->shuffle();
// 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:

<?php

$featured = page('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>
</div>

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

<?php

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

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

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 cooler. 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.

$page->images()->shuffle();
// this will return a random set of images for the current page

$page->images()->shuffle()->first();
// this will return only one random image for the current page

$page->images()->shuffle()->limit(3);
// 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:

$images = page('portfolio/project-a')->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:

$randomImage = page('portfolio')
  ->children()
  ->shuffle()
  ->first()
  ->images()
  ->shuffle()
  ->first();

Author