Using first() in array for homepage article

This may not be the best way to accomplish this, but I’m trying to retrieve the first article from a particular section to publish to the homepage. I’ve set this up via a “Home” checkbox on the article page. Right now there are two articles published to the homepage and when the ->first() is removed from the code below, it correctly display both titles. I’m just trying to display the first article. Any ideas?

<?php foreach(page('talk-and-listen')->children()->visible()->sortBy('date', 'desc') as $child): ?>
 <?php if($child->home()->bool()): ?>
   <?php echo $child->first()->title()->html() ?>
<?php endif ?>
<?php endforeach ?>

First() only works with a collection, not with a single page. You can, however, filter the collection based on the home field using filterBy() and then pick the first.

<?php 
$page = page('talk-and-listen')->children()->visible()->sortBy('date', 'desc')->filterBy('home', 'yes')->first();
?>
1 Like

Ahh, makes sense. Thanks!