Latest article as featured from collection

Good morning (swiss morning…)!

I have three blogs on my website (notes, videos and music), from which I fetch the article on the front-page with a collection.

Collection

return function ($site) {
    return $site->find('notes','music','videos')->children()->listed()->sortBy(function ($page) {
  return $page->date()->toDate();
}, 'desc');
};

Articles are each styled per category, this works fine. But on top of that, I want to style the latest article of this collection differently. I tried to solve this with <?php if($article->isFirst()): ?>, the problem is, this method selects the first article of each blog/category within the collection, which results in three featured articles.

How could I select only the latest article in the collection, no matter which category?

Use indexOf():

$collection = collection('articles'); // or whatever your collection is called
foreach ($collection as $article) {
  if ($collection->indexOf($article) === 0) {
    echo 'This is the first article';
  }
} 
2 Likes

Works exactly as I need it - thanks a lot Texnixe!