Let’s create this snippet in site/snippets/recent-posts.php and bring the list view to life, step by step. First, we need to collect all post items that we want to list in a variable:
// Get all child pages in the "posts" page
$allPosts = $pages->get('posts')->children()->visible();
We ask the global $pages object for the page named “posts” and request all of its child pages that are visible (i.e. numbered).
However, we want all but the newest item (because we display this separately at the top of the homepage in special markup).
// we want all but the latest item
$recentPosts = $allPosts->flip()->slice(1);
“flipping” the collected pages will sort them newest to oldest; and using the slice() function, we remove the first object (the newest one) from the set.
We can then simply cycle through the items and output what we want to display:
With a page object’s image() method, you can search for a file in that page’s folder. Since we have placed a “teaser.jpg” image in the “05-chevron” folder, we can easily get hold of it. Using the url() method on the resulting image object allows us to feed this into a good old <img> tag.