Ordered By Newest Without Adding <time> Format?

Dear All

Here is my current code on my list.html

{{ partial "head.html" . }}
{{ partial "header.html" . }}

<main class="main">

{{ $paginator := .Paginate (.Pages) }}
{{ range $paginator.Pages }}

<article class="post-entry">
<header class="entry-header">
<h2>{{ .Title }}</h2>
<section class="entry-content">
<p>{{ .Summary }} ... </p>
<a class="entry-link" href="{{ .Permalink }}"></a>
{{ end }}

{{ if gt $paginator.TotalPages 1 }}
<footer class="page-footer">
<nav class="pagination">
<a class="prev" href="{{ if $paginator.HasPrev }}{{ $paginator.Prev.URL }}{{ end }}">« Prev</a>
<a class="next" href="{{ if $paginator.HasNext }}{{ $paginator.Next.URL }}{{ end }}">Next »</a>
{{ end }}


{{ partial "footer.html" . }}

I don’t use any <time> format on layouts. So, it works for all sections ordered by title alphabetical. How to make it works ordered by newest post without adding <time> format on layouts?


The only thing that comes to my mind would be to have a look at:

This function returns Go’s os.FileInfo and within that interface there is ModTime() time.Time // modification time

Don’t know if it is possible to order a list by file modification time in Hugo.

You may try and see for yourself.

Of course you would also need to refactor your list templates and use:

However the above approach seems like a super complicated course of action.

I would only consider the above if I had thousands of content files and I didn’t want to manually enter a date parameter but even then the usual methods of ordering lists (e.g. By Last Modified Date) are simpler than Local File Templates by far…

And that’s just my opinion.

1 Like

You have to create $paginator with the .Pages.GroupByPublishDate

You can steer it in the config file (ex.:slight_smile:

date                       = [ "date", ":filename", ":default"]
publishDate                = [ "publishDate", "date"]
lastmod                    = [ "lastmod", ":fileModTime", "publishDate"]
expiryDate                 = [ "expiryDate"]
1 Like

Thanks for support. I tried lastmod method before. But the problem is, everytime I make an edit to old post, that old post will bumped to the first page.

Have you tried?

1 Like

Thanks for support. I will try it. By the way, I don’t use any <time> on layouts to avoid date stamp appear on website and search engine result.

Yes, I tried it. But the result is on pagination (prev next button) go from one section to another section, due to same published date. Maybe I have to try .GroupByPublishDate.Reverse to make it appear by newest post without crossing section. Or create different list layout for each section.