Get to page from a checkboxes field

Hi everyone, I am trying to get something to work, but got stuck here, so would appreciate any help!
I have an Intro page where I want to present selected projects pages, so I am using the ‘Checkboxes’ field to make a list selection. In my template I can get the checked options but I can’t get to the selected pages. Also ‘toPage()’ and other methods I’ve tried didn’t work.

My blueprint:

pages_vids:
        type: checkboxes
        options: query
        query: kirby.page('works').children.published

This is a simple example of my snippet:

<?php
$items = $site->find('intro')->pages_vids()->split();
foreach ($items as $item): ?>
  <h1><?= $item->toPage()->title() ?></h1>
<?php endforeach ?>

Would appreciate the help :pray:

This will work:

$items = $site->find('intro')->pages_vids()->toPages(',');
if ($items->isNotEmpty()): 
foreach ($items as $item): ?>
  <h1><?= $item->title() ?></h1>
<?php endforeach ?>
<?php endif ?>

An additional if-statement that checks if the intro page exists is recommendable as well.

Why does your code not work?

<?php
// with the split method, you create an array of page id values
$items = $site->find('intro')->pages_vids()->split();
// you loop through that array, but `$item` is a simple string
foreach ($items as $item): ?>
<!-- here you try to convert this string into a page, this won't work,
because you can't call a page method (`toPage()`) on a string -->
  <h1><?= $item->toPage()->title() ?></h1>
<?php endforeach ?>

Thanks so much @texnixe! Saved me as always :slight_smile:

Makes perfect sense, just couldn’t find an answer how to get it to work :slight_smile: Thanks for the explanation as well!