[SOLVED] Get the first and last post in a section (date-based)

That’s not a single page that @jgreely is grabbing. He’s grabbing the index page; i.e., the page with the kind of section. That’s the cool part about the .GetPage function. Try this instead if you are looking specifically for posts:

<!--Sorts pages by the publishdate field in front matter; you could use by date as well; the reverse puts the newest at the front-->
{{$posts := ($.Site.GetPage "section" "posts").Pages.ByPublishDate.Reverse}}
<!--Grab the most recent-->
{{ range first 1 $posts }}
<p>Most recent: {{.Title}} {{.PublishDate}}</p>
{{ end }}
<!--Grab the oldest-->
{{ range last 1 $posts }}
<p>Oldest: {{.Title}} {{.PublishDate}}</p>
{{ end }}

Also @jgreely 's solution will work if done within, for example, a partial that is embedded in a single.html since .Section will pull from the value for that particular rendered page…

1 Like