Sort by lastmod

Hello everyone,
I would like to sort by lastmod instead of published time.
Is it possible?

I’m using the no longer maintained story theme of Mr Schwartz

Yes, this example is shown in the Lists of Content documentation.

Below you’ll see the .Pages.ByLastmod declaration.

<ul>
    <!-- orders content according to the "lastmod" field in front matter -->
    {{ range .Pages.ByLastmod }}
        <li>
            <h1><a href="{{ .Permalink }}">{{ .Title }}</a></h1>
            <time>{{ .Date.Format "Mon, Jan 2, 2006" }}</time>
        </li>
    {{ end }}
</ul>
1 Like

Thank you,
I guess that at the story theme the code should be placed at the layouts/partials/li.html.
But how can I combine it with the already present configuration?
Whatever I’ve tried hasn’t applied to the ordering or has broken the design or ended up with a hugo syntax error.

The theme seems to be somehow complicated with all these added features :frowning:
I can’t change that. It was the best solution when I needed a starting point.

At the moment there is:

<h1 class="f3 fw1 mt0 lh-title"><a href="{{ .Permalink }}" class="color-inherit dim link">
{{- if eq .Section "talks" }}{{ .Params.event }}: {{ end -}}
{{- cond (eq .Site.Params.titlecase true) (.Title | title | markdownify) (.Title | markdownify) -}}
</a></h1>

See here the full li.html

{{- $hdr := cond (or (in .Site.Params.classes "feature-nohdr") (in .Params.classes "feature-nohdr")) false true -}}

<div class="w-100">
<article class="bb b--black-50">
<div class="db pv4 {{ cond $hdr "ph3" "" }} ph0-l no-underline dark-gray">
<div class="flex flex-column flex-row-ns">
{{ if $hdr }}
<!--<div class="mb4 mb0-ns w-100 h4 h4-m w4-m h5-ns w5-ns br3 cover {{ if lt .Date.Unix now.Unix }}hide-child{{ end }}" style="background-image: url('{{ absURL (default "img/default-header-img.tn-500x500.jpg" (default .Params.image .Params.thumbnail)) }}'); background-position: center;">-->
<div class="mb4 mb0-ns w-100 h4 h4-m w4-m h5-ns w5-ns cover {{ if lt .Date.Unix now.Unix }}hide-child{{ end }}" style="background-image: url('{{ absURL (default "img/radszene-header.png" (default .Params.image .Params.thumbnail)) }}'); background-position: center;">
<!--thumbnail date-->
<a href="{{ .Permalink }}" class="db child w-100 h-100 f7 lh-copy white no-underline br3 ph3 pt4 pt5-l ttu tc bg-black-30">
<p>Veröffentlicht<br>
<i class="mt3 mt5-l child far {{ cond (gt .Date.Unix now.Unix) "fa-hourglass" "fa-calendar-alt" }}"> {{ .Date.Format "Jan 2, 2006" }}</i>
</p>
<p>Zuletzt überarbeitet<br>
<i class="mt3 mt5-l child far {{ cond (gt .Date.Unix now.Unix) "fa-hourglass" "fa-calendar-alt" }}"> {{- .Lastmod.Format "Jan 2, 2006" -}}</i>
</p>
</a>
</div>
{{ end }}

<div class="w-100 w6-ns pl3-ns">

<h1 class="f3 fw1 mt0 lh-title"><a href="{{ .Permalink }}" class="color-inherit dim link">
{{- if eq .Section "talks" }}{{ .Params.event }}: {{ end -}}
{{- cond (eq .Site.Params.titlecase true) (.Title | title | markdownify) (.Title | markdownify) -}}
</a></h1>

<div class="pv2">
{{ if .Params.tags }}<strong>Tags:</strong> {{range .Params.tags}}<a href="{{ "/tags/" | relLangURL }}{{ . | urlize }}">{{ . }}</a> {{end}}{{end}}
</div>

<div class="pv2">
{{ if .Params.media }}<strong>Medientyp:</strong> {{range .Params.media}}<a href="{{ "/media/" | relLangURL }}{{ . | urlize }}">{{ . }}</a> {{end}}{{end}}
</div>

<div class="f6 f5-l lh-copy nested-copy-line-height nested-links">
{{ if eq .Section "slides" }}
{{ .Description | markdownify }}
<!--<p class="pa0 sans-serif f7"><a class="ba br3 pa2 link" href="{{ .Permalink }}">View Slideshow <i class="fas fa-chevron-circle-right"></i></a></p>-->
{{ else if eq .Section "talks" }}
{{ .Summary }}
<!--<p class="pa0 sans-serif f7"><a class="ba br3 pa2 link" href="{{ .Permalink }}">Talk Details <i class="fas fa-chevron-circle-right"></i></a></p>-->
{{ else }}
{{ .Summary }}
<!--<p class="pa0 sans-serif f7"><a class="ba br3 pa2 link" href="{{ .Permalink }}">Continue Reading <i class="fas fa-chevron-circle-right"></i></a></p>-->
{{ end }}
</div>
</div>
</div>
</div>
</article>
</div>

Here is the relevant (guess) part of my baseof.html

<div id="hdr" class="tc-l {{ cond $hdr "pv4-ns pv5-l" "" }} pv2 ph3 ph4-ns">
<h1 class="{{ cond $hdr "near-white mt1-ns" "" }} f2 fw3 mb0 mt0 lh-title">{{ cond (eq .Site.Params.titlecase true) (.Title | title | markdownify) (.Title | markdownify) }}</h1>
<h2 class="{{ cond $hdr "near-white mt3-l mb4-l" "" }} fw1 f6 f3-l measure-wide-l center lh-copy mt2 mb3">
<!-- You can put a subtitle here in _index.md parameters, or it'll get
filled in with date and categories if it's a post -->
{{ if .IsPage }}
{{ if eq .Section "talks" }}
<!--Presented
<time datetime="{{ .Date.Format "2006-01-02T15:04:05Z07:00" }}">
{{- .Date.Format "January 2, 2006" -}}
</time>-->
<!--at <a class="no-underline category {{ cond $hdr "near-white dim" "black hover-gray" }}" href="{{ .Params.site }}">{{ .Params.event }}</a>-->
{{ else }}
Veröffentlicht
<time datetime="{{ .Date.Format "2006-01-02T15:04:05Z07:00" }}">
{{- .Date.Format "Jan 2, 2006" -}}
</time>
<br>
Zuletzt überarbeitet
<time datetime="{{ .Date.Format "2006-01-02T15:04:05Z07:00" }}">
{{- .Lastmod.Format "Jan 2, 2006" -}}
</time>
<!--<span class="display-print">by {{ .Params.author | default .Site.Params.author }}</span>
{{ range $idx, $cat := .Params.categories }}{{ if gt $idx 0 }}, {{ end }}{{ if eq $idx 0 }} in {{ end }}<a href="{{ "categories/" | absURL }}/{{ . | urlize }}" class="no-underline category {{ cond $hdr "near-white dim" "black hover-gray" }}">{{ . | title }}</a>{{ end }}
<span class="display-print">at {{ .Permalink }}</span>-->
{{ end }}
{{ else }}
{{ with .Params.subtitle }}{{ . | markdownify }}{{ end }}
{{ end }}
</h2>
</div>

<!-- if there's a "credits" parameter in front matter there'll be a camera icon here -->
{{ if $hdr }}
{{ with .Params.credit }}
<div class="w-100 cf hide-print">
<a class="fr f6 ma0 pa2 link white-50 dim fas fa-camera" href="{{ . }}" title="Photo Credit"></a>
</div>
{{ end }}
{{ end }}

</div>

Hmm, Nothing I tried worked.
Do I need to enter the lastmod date for every post manually?
The lastmod is displayed correctly, just the ordering doesn’t work.

I’m not using github. Is there nevertheless any about it to add to config.toml?

Would it help if I upload my site to github?

Yes. Otherwise we’re both just guessing.

1 Like

Do I upload the whole folder?
With all content and all personal data like imprint, GDPR etc?

Sorry, you need to learn about Git and GitHub on your own. This is the wrong forum for that.

here it is:

This is an incomplete site that doesn’t build.

give me some minutes

You can build it now. I downloaded it and it works with “hugo server”

config.toml

lastmod = ["lastmod", ":fileModTime", ":default"]

Because your content does not contain lastmod in front matter, it is falling back to the file system’s last modification time. When you look at your files in a file listing, are the modification dates what you expect?

layouts/_default/list.html

diff --git a/layouts/_default/list.html b/layouts/_default/list.html
index 15afbc9..7187530 100644
--- a/layouts/_default/list.html
+++ b/layouts/_default/list.html
@@ -13,7 +13,7 @@
       {{ partial "li.html" . }}
     {{ end }}
   {{ else }}-->
-    {{ range where (where $pages ".Date.Unix" "<" now.Unix) "Section" "ne" "slides" }}
+    {{ range (where (where $pages ".Date.Unix" "<" now.Unix) "Section" "ne" "slides").ByLastmod.Reverse }}
       {{ partial "li.html" . }}
     {{ end }}
   <!--{{ end }}-->

layouts/index.html

diff --git a/layouts/index.html b/layouts/index.html
index 30110e3..37de418 100644
--- a/layouts/index.html
+++ b/layouts/index.html
@@ -6,8 +6,11 @@
 	</section>
 
 	<section class="w-100 mw8">
-		{{ $pages := where .Paginator.Pages ".Params.skip" "ne" "true" }}
-		{{ range where (where $pages ".Date.Unix" "<" now.Unix) "Section" "ne" "slides" }}
+		{{ $pages := where site.RegularPages "Params.skip" "ne" true }}
+		{{ $pages = where $pages "Date.Unix" "<" now.Unix }}
+		{{ $pages = where $pages "Section" "ne" "slides" }}
+		{{ $pages = $pages.ByLastmod.Reverse }}
+		{{ range (.Paginate $pages).Pages }}
 			{{ partial "li.html" . }}
 			{{ end }}
 	</section>

1 Like

Unfortunately ordering by modified file date hasn’t worked so far.
This would be the base case for me.
On top the possibility to be able to hardcode the lastmod in frontmatter would be perfect.
I’m going to try out your snippets.

It works like a charm :star_struck:

It is ordering by file date but also with higher priority if I manually set lastmod in frontmatter!

Thank you very much !!

1 Like

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