Display the last article sort by date

Hi,
Sorry for a basic question, but I didn’t find the answer neither in the docs nor in the forum.
I would like to display on the home page the last article (sort by date) amongst all articles in the content directory (whatever the location in sub directory)
I try the following code but it seems that it doesn’t work

<?php foreach($site->children()->visible()->sortBy('date', 'desc')->limit(1) as $article): ?>
<article>
  <h1><?php echo html($article->title()) ?></h1>
  <p><?php echo excerpt($article->text(), 300) ?></p>
  <a href="<?php echo $article->url() ?>">Read more…</a>
</article> 

Thank you for your help

You can use $site->index()->visible() ... to get all visible pages regardless of their folder position … You may want to use first() instead of limit()

Thank you for your swift answer.
I use the following code which works fine.

<?php $article = $site->index()->visible()->sortBy('date', 'desc')->first() ?>

If I may, just after that I would like to display the second and third articles but in a different div (format).
Is it a simple way to do it ?
Thank you

1 Like

Luckily, there is the nth() method http://getkirby.com/docs/cheatsheet/pages/nth