How to sort posts on PublishDate and use .PrevInSection and .NextInSection with that

I want to sort my blog posts based on publish date, so that I can show .Date as describing the date that I started writing the post, and .PublishDate as the date that the post actually exists publically.

Right now, a regular {{ range .RegularPages }} sorts based on .Date, not .PublishDate.

Have a look at this topic, as it describes the general methods that can be used on single page templates, to change the sorting order of pages called with .PrevInSection, .NextInSection etc.

1 Like

Thanks a lot! I used a modified form of his code snippet:

<section class="post-navigation">
{{/*We get the posts within the section and then sort by publish date in descending order.*/}}
{{ $pages := (sort (where site.RegularPages "Section" .Section) ".PublishDate" "desc") }}
  <div>{{ with $pages.Prev . }}{{ if .PublishDate }}<a href="{{.Permalink}}">
      {{ with .Title }}{{ . }}{{ else }}{{ .Summary | truncate 70 }}{{ end }}</a> {{ end }}{{ end }}</div>
  <div>{{ with $pages.Next . }}{{ if .PublishDate }}<a
      href="{{.Permalink}}">{{ with .Title }}{{ . }}{{ else }}{{ .Summary | truncate 70 }}{{ end }} </a> {{ end }}{{ end }}</div>
{{ end }}
</section>

Thanks a lot again!

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.