# Shuffle files in homepage

**URL:** <https://forum.getkirby.com/t/shuffle-files-in-homepage/17590>\
**Category:** Questions\
**Tags:** v3\
**Created:** [March 12, 2020, 12:31pm UTC](https://forum.getkirby.com/t/shuffle-files-in-homepage/17590 "2020-03-12T12:31:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![cbtr](https://avatars.discourse-cdn.com/v4/letter/c/cc9497/32.png) [@cbtr](https://forum.getkirby.com/u/cbtr)\
**Post date:** [March 12, 2020, 12:31pm UTC](https://forum.getkirby.com/t/shuffle-files-in-homepage/17590/1 "2020-03-12T12:31:35Z")

</div>

Hi,

I’m trying to use the `->shuffle()` [function](https://getkirby.com/docs/cookbook/content/random-content#random-files-and-images) to get a random image from my homepage blueprint.

I have created the under the folder **site/blueprints/pages/** a **home.yml** inside. I have differents tabs, with some text fields, files fields and so on.

I want to use the shuffle on that particular files fields:

```
    fields:
      gallery:
        label:
          fr: galerie
          en: Gallery
        type: files
        accept: image/jpeg, image/png, image/gif
        layout: cards
        template: image
        translate: false

```

To output the random image I use the following code:

```
$site->homePage()->gallery()->shuffle()->first()->toFile();

```

But the same and first image always appears… Because I have other images in the others tabs I can’t use the `->images()` instead of the `->gallery()`

Any help would be grateful!

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [March 12, 2020, 12:45pm UTC](https://forum.getkirby.com/t/shuffle-files-in-homepage/17590/2 "2020-03-12T12:45:51Z")

</div>

You have to convert the field values to a files collection first using `toFiles()` as described in the [documentation for the field](https://getkirby.com/docs/reference/panel/fields/files#how-to-use-in-templates-snippets).

```auto
$site->homePage()->gallery()->toFiles()->shuffle();

```

If you only want to get the first image of the shuffled collection:

```auto
// use if statement to make sure we have an image object
if ($image = $site->homePage()->gallery()->toFiles()->shuffle()->first()) {
  echo $image;
}

```

---

<div class="post-metadata">

**Author:** ![cbtr](https://avatars.discourse-cdn.com/v4/letter/c/cc9497/32.png) [@cbtr](https://forum.getkirby.com/u/cbtr)\
**Post date:** [March 12, 2020, 12:50pm UTC](https://forum.getkirby.com/t/shuffle-files-in-homepage/17590/3 "2020-03-12T12:50:09Z")

</div>

> [@texnixe](#):
>
> -\>toFiles()

Thank you, I see now the logic behind the `->toFiles`.
