Struggeling with collection


I am struggeling big time with the collection. I tried stuff from the docs but I cannot get it to work.

I have made a site with a page with updates (just a blog).

this is how my updates template file looks:

So far so good. That works.
Now I want a selection of the updates showing on the updates page on the default page.
Like a collection.
See screenshots below.
What should I add for this in my collection file to get this working?

Thnx in advance.

You need to loop throught the collection, just like you do in your updates template.

foreach ($kirby->collection('updates') as $update) {
 // do stuff

In the future, please post code as code snippets for easy copy/paste, not screenshots, thanks.

My bad sorry.

I dont get it, should I write html inside that part in the collection file.
or something like this?:

<?php foreach ($kirby->collection('updates') as $update) {

return function () {
    return page('updates')
        ->sortBy('date', 'desc');

No, the foreach doesn’t belong in the collection file, there you just return the pages that should be in your collection.

I was referring to you this part in default.php


Which has no php tag, no foreach, no nothing.

The rest is in the docs:

Im sorry the docs page is unclear to me. Tried multiple of the snippets.
I will watch the video.

What exactly is unclear?

There are two parts to it

  1. Define the collection under /site/collections
  2. Use the collection with a foreach loop in your templates/snippets like you would loop through any other pages collection (or users or files collection)

I’m sorry I am really noob at this.

What do I define in collection.php?
How do I use the collection in default.php file exactly?

When I try it from docs its just renders “$kirby->collection(“updates”);” in text to the page

As I already wrote above:

Collection defintion site/collections/updates.php


return function ($site) {
    return $site->find('updates')->children()->listed()->sortBy('date', 'desc');

Then in your template the code I posted above:

<?php foreach ($kirby->collection('updates') as $update): ?>
  <!-- here goes your code -->
<?php endforeach ?>

I tried the following.

In my template:

<?php foreach ($kirby->collection('updates') as $update): ?>
    <div class="swiper-slide news-card">
        <div class="swiper-slide__container">
            <div class="news-card__header">
                <img class="news__image" src="" loading="lazy" alt="lorem">
            <div class="news-card__section">
                <span class="news-card__date">01.01.2023</span>
                <p class="news-card__title">
                    <strong><?= $update->title() ?></strong>
                <p class="news-card__description"><?= $update->description() ?></p>
                <a class="news-card__link" href="<?= $update->url() ?>">
                    <div class="icon icon-arrow-white-right"></div>
<?php endforeach ?>

In my collection file:


return function ($site) {
    return $site->find('updates')->children()->listed()->sortBy('date', 'desc');

It still renders nothing to the template file on the front end.
Could it be because of the sortBy.
What am I doing wrong?

Because your updates folder has no listed subpages…

I get it that works now!
My last question:
How do I exclude 1 item (most recent one)?

Already when creating the collection or when fetching the collection?

When fetching
(in that collection I want to exclude 1 item)

return function ($site) {
    return $site->find('updates')->children()->listed()->sortBy('date', 'desc')->offset(1);

Thanx soooo much!!! you are amazing!
This topic can be closed