I have a multilingual site where I usually post in English but sometimes I’ll have a post in Dutch as well. I also have some posts that I wrote in both languages (index.md
and index.nl.md
).
I want to list my most recent blog posts in either languages. But doing so, I don’t want a translated article to pop up twice.
So far, I’ve come up with the following template to show me the most recent posts in any language:
<ul>
{{ range where (where .Site.AllPages "Section" "==" .Params.section) "Kind" "==" "page" | first .Params.limit }}
<li class="bullet">
<a href="{{ .RelPermalink }}" title="{{ .Title }}" hreflang="{{ .Language.Lang }}">{{ .LinkTitle }}</a>
</li>
{{ end }}
</ul>
I 'm using AllPages
since that will collect pages from all languages, where RegularPages
would only show you pages in your current language.
Because of that, I need to filter out the page
kind, otherwise it’ll also show e.g. the section
page.
There’s a filter on section here as well, as I tried to make the filter dynamic so that I can select different sections if I wanted to. But for testing this could be hardcoded to e.g. blog
.
content/
├─ blog/
│ ├─ topic 1/
│ │ ├─ index.md <-- English
│ │ ├─ index.nl.md <-- Dutch
│ ├─ topic 2/
│ │ ├─ index.md <-- English
│ ├─ topic 3/
│ │ ├─ index.nl.md <-- Dutch
The resulting list is
- topic 1 <--- English version
- topic 1 <--- Dutch version
- topic 2 <-- English
- topic 3 <-- Dutch
While I would like to get
- topic 1 <--- current site language version
- topic 2 <-- English
- topic 3 <-- Dutch
A simplified version of my full menu template is:
<nav>
<ul class="sidebar-nav">
{{- range .Site.Menus.main.ByWeight }}
<li class="heading">
<a href="{{ .URL }}" title="{{ or .Title .Name }}" {{- with .Params.rel }} rel="{{ . }}"{{ end -}}>
{{ .Name }}
</a>
</li>
{{ if .Params.section }}
{{ with .Pre }}
<li class="sub-heading">
{{ . }}
</li>
{{ end }}
{{ range where (where $.Site.AllPages "Section" "==" .Params.section) "Kind" "==" "page" | first .Params.limit }}
<li class="bullet">
<a href="{{ .RelPermalink }}" title="{{ .Title }}" hreflang="{{ .Language.Lang }}">{{ .LinkTitle }}</a>
</li>
{{ end }}
{{ end }}
{{ end }}
</ul>
</nav>
menus:
main:
- name: About
pageRef: /about/
weight: 10
- name: Blog
pageRef: /blog/
weight: 20
pre: Recent
params:
section: blog
limit: 3