# How to make first file as cover image?

**URL:** <https://forum.getkirby.com/t/how-to-make-first-file-as-cover-image/15978>\
**Category:** Questions\
**Tags:** v3\
**Created:** [October 17, 2019, 5:48pm UTC](https://forum.getkirby.com/t/how-to-make-first-file-as-cover-image/15978 "2019-10-17T17:48:41Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Roman](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/roman/32/6571_2.png) [@Roman](https://forum.getkirby.com/u/Roman)\
**Post date:** [October 17, 2019, 5:48pm UTC](https://forum.getkirby.com/t/how-to-make-first-file-as-cover-image/15978/1 "2019-10-17T17:48:41Z")

</div>

Hi, I am using this model to have cover image from files section:

```
class BlogpostPage extends Page
{
    public function cover()
    {
        return $this->content()->get('cover')->toFile() ?? $this->image();
    }
}

```

As I understand, it’s taking file, that is first by filename, but maybe it’s possible to show the one, that is first by sort order?

 ![first-file](https://europe1.discourse-cdn.com/flex017/uploads/getkirby/original/2X/4/47343116b0dd352133c8b111516ee07e4e31c753.png)

---

<div class="post-metadata">

**Author:** ![thguenther](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/thguenther/32/16708_2.png) [@thguenther](https://forum.getkirby.com/u/thguenther)\
**Post date:** [October 17, 2019, 6:10pm UTC](https://forum.getkirby.com/t/how-to-make-first-file-as-cover-image/15978/2 "2019-10-17T18:10:15Z")

</div>

You can use the following to get the first file:

```php
class BlogpostPage extends Page {
    public function cover() {
        if($this->hasImages()) {
            return $this->images()->sortBy('sort', 'asc')->first();
        }
    }
}

```

(untested)

I tend to use a page method plugin for stuff like that as it’s not bound to one template/model:

```php
Kirby::plugin('my/methods', [
    'pageMethods' => [
        'cover' => function() {
            if($this->hasImages()) {
                return $this->images()->sortBy('sort', 'asc')->first();
            }
        }
    ]
]

```

---

<div class="post-metadata">

**Author:** ![Roman](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/roman/32/6571_2.png) [@Roman](https://forum.getkirby.com/u/Roman)\
**Post date:** [October 17, 2019, 6:18pm UTC](https://forum.getkirby.com/t/how-to-make-first-file-as-cover-image/15978/3 "2019-10-17T18:18:24Z")

</div>

Checked the first one, working fine, thank you! Also checked second, fixed some syntax errors, but still seems not to work.

---

<div class="post-metadata">

**Author:** ![Roman](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/roman/32/6571_2.png) [@Roman](https://forum.getkirby.com/u/Roman)\
**Post date:** [October 17, 2019, 6:23pm UTC](https://forum.getkirby.com/t/how-to-make-first-file-as-cover-image/15978/4 "2019-10-17T18:23:30Z")

</div>

Also tried adding “cover” field (in case editor wants to override first image and choose custom cover image), it’s still showing first image, is it possible to show what is in cover field, if it’s not empty?

![cover](https://europe1.discourse-cdn.com/flex017/uploads/getkirby/original/2X/a/a0cc4acaa3a15a8f5b5bf63790b696d756593d48.png)

---

<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:** [October 17, 2019, 6:26pm UTC](https://forum.getkirby.com/t/how-to-make-first-file-as-cover-image/15978/5 "2019-10-17T18:26:10Z")

</div>

Yes:

```auto
class BlogpostPage extends Page {
    public function cover() {
        return $this->content()->get('cover')->toFile() ?? $this->images()->sortBy('sort', 'asc')->first();
    }
}

```

```auto
<?php if ($cover = $page->cover()): ?>
<?= $cover ?>
<??php endif ?>

```

Note that a page method won’t override a page model, so adding a page method with the same name as an existing page model method won’t have any effect (at least not on pages that use the model).

---

<div class="post-metadata">

**Author:** ![Roman](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/roman/32/6571_2.png) [@Roman](https://forum.getkirby.com/u/Roman)\
**Post date:** [October 17, 2019, 6:29pm UTC](https://forum.getkirby.com/t/how-to-make-first-file-as-cover-image/15978/6 "2019-10-17T18:29:23Z")

</div>

Ah, I see, understand now, perfect, thank you! So now it’s showing first image from files section, or cover, if it’s selected, very nice.
