Retrieving content from subpages and sort everything by date

Hei all!

I am currently working on my photography website and have some issues with a latest view on the startpage: I am trying to get the latest galleries to display one on the startpage. “Date” is a field in gallery.txt inside the galleries-folders.

My structure is as follows (example):


At the moment, I get the galleries entries displayed as thumbs with links using this code for testing purposes:

<? foreach(page('bands')->children()->visible() as $bandcollection): ?>
    <? foreach($bandcollection->children()->visible() as $bandcollection2): ?>
        <? foreach($bandcollection2->children()->invisible()->sortBy('date','desc') as $bands): ?>
        <div class="column">
            <? if($image = $bands->images()->sortBy('sort', 'asc')->first()): ?>
            <a href="<?= $bands->url() ?>" title="<?= $bands->title() ?>">
                      <img src="<?= $image->crop(400)->url(); ?>" alt="<?= $image->name() ?>">
                      <figcaption><?= $bands->title()->html() ?><br><time datetime="<?= $bands->date('c') ?>"><?= $bands->date('d.m.Y') ?></time></figcaption>
            <? endif ?>
        <? endforeach ?>
    <? endforeach ?>
<? endforeach ?>

The code works and displays the link to the gallery and a thumb from it. It sorts the links within the confines of a folder (e.g. 01-bandwithnamea01) – I instead need to get all galleries sorted by date.

I am not sure if there is a more easier and cleaner method to get the content from my galleries deep down in my structure - I have already looked for a cleaner solution inside Kirby’s documents, but did not find anything suitable.

I am not sure if I have to add all subfolders to an array now (with another foreach-loop).

Probably I have been staring too long on my screen today and I am stuck in a rut. :wink:

Any pointers to a better solution are welcome. :slight_smile:

Thanks in advance,


I would simply filter an index of the bands page by template and sort by date:

$galleries = page('bands')->index()->filterBy('intendedTemplate', 'gallery')->sortBy('date', 'asc');
1 Like

Oh gods, how did I overlook this? Thank you for the pointer – it seems to work on my test page. :smiley:

Over one and a half hour trying different approaches and it’s quite simple. If we ever meet, remind me to buy you a beer or wine (or tea or coffee). :smiley:

1 Like