Pulling content not working as intended

For last hour, I’m trying to debug an error about fetching content from a subpage. I’ve got this folder structure:

Now I’m trying to fetch the content of alert.txt file (there is template and blueprint for that too). The content of alert txt is this:

Title: Herbsturlaub 2021
----
Specialmessage: Hey da ist was!
----
Datepublish: 
----
Dateexpire:

The information should be fetched from anywhere on the site, so I wrote this snippet:

<?php foreach ($site->index()->filterBy('template', '==', 'alert') as $item): ?>
<?= $item->specialMessage()->kirbytext() ?>
<?php endforeach ?>

And… it doesn’t work. But what definitely bugs me is that I’m using this same snippet in similar fashion for another template, and it’s working fine as intended. May anyone have an idea why fetching won’t work on this example?

Do you really want to get a collection of pages (i.e. all children of the sondermeldungen page or only the contents of that particular subpage?

If you want to fetch all children of sondermeldungenI’d do it like this (to prevent going through the complete index:

<?php
if ($p = page('sondermeldungen')) {
  foreach ($p->children()->listed()->filterBy('intendedTemplate', 'alert') as $item) {
    echo $item->specialMessage()->kirbytext();
  }
}

If all the children in that folder use the same alert template, then you can remove the filter.

If for some reason you really want to filter the complete index, then replace template with intendedTemplate in your code snippet above, but keep in mind that it should be avoided if not really necessary.

1 Like

Thanks for reply! That helped me, especialy about optimization. I’m going to use this only on page ‘sondermeldungen’ like you described.

What still bugs me, why the original snippet I used for another content (only difference being different template) worked but it didn’t for the template ‘alert’.

I’m definitely going to change another snippets too not to plow through whole index.