I’m trying to get a link to the latest blog post but can´t really get it right, any ideas?
Where do you want to link to it, in a template or in a text file?
On a template, I want that when clicking “Blog” it shows the last article
OK, here you go, the first()
method fetches the first item of a collection:
<?php
$latestsPost = page('blog')->children()->visible()->sortBy('date', 'desc')->first();
echo $latestPost->title(); //etc.
?>
3 Likes
THanks!, tweaked it a bit and this works:
<?php echo page('blog')->children()->visible()->sortBy('desc')->last()->url()?>
Well, maybe it works, but your syntax is not correct, sortBy()
expects a field to sort by, so I guess this is ignored and you can just leave it out completely:
$pages->sortBy($field, $direction = 'asc', $method = SORT_REGULAR)
1 Like