Calling pages with Toggle 'yes'

Hello,

I’m trying to created a ‘Featured Projects’ page and have read many helpful threads on here about filterBy, filter and callbacks, but am still doing something wrong and would appreciate some pointers.

‘Featured’ is a ‘yes/no’ toggle in the blueprint for each ‘Project’ page in ‘Projects’.
I’m trying to get the first image or every visible page in Projects that has the Toggle as Yes:

<?php
$projects = page('projects')->children()->visible()->filter(function ($child) {return str::contains($child->featured() == 'yes') ? true });
foreach($projects as $project): ?>
    <?php if ($image = $project->images()->sortBy('sort', 'asc')->first()): ?>
      <img src="<?php echo $image->url() ?>" >
     <?php endif ?>
<?php endforeach ?>`

Many attempts has led to many different errors. The current one is
Parse error: syntax error, unexpected '}'
So I think I’m just messing up the callback? Where am I going wrong?

Try this_

$projects = page('projects')->children()->visible()->filter(function ($child) {
  return $child->featured()->bool();
});

BTW: Please help to improve code readability by enclosing blocks of code withing three backticks at the beginning and the end of a block on a separate line. I have corrected your code above. To see how it works, click on the pencil icon of your post. Thank you.

1 Like

Perfect! Still in my early days for PHP and I value your help.
And thank you for editing my code - I was trying to edit with four spaces and failing.

Thank you for all your work on these forums :relaxed: