Override the template for permalink?

I feel like I’m missing something simple. I have a slug setup for my posts like /posts/:year/:slug which works great. I can easily override the template used by the post in layouts/posts/single.html. The problem I cannot seem to figure out is how to create a template for the :year element. At the moment /posts/:year results in a generated 404.

I tried creating a year taxonomy to see if I could do it that way, but that did not seem to work either.

Again, just looking to create the template for /posts/:year and ideally use it to iterate over posts created during that year.

Can you reorganize your content to be:

└── posts/
    └── 2023/
        ├── _index.md
        ├── post-1.md
        └── post-2.md

ah yeah I suppose that’d work. I’ve got the content organized like this at the moment:

content /
└── posts/
    └── 2023-06-23-my-awesome-title.md

(I also have no _index.md)

But I could see how your suggestion would solve it.

Out of curiosity is it possible to accomplish this without the reorganization?

hugo new posts/2023/_index.md
hugo new posts/2022/_index.md
hugo new posts/2021/_index.md


title = 'Posts in 2021'
date = 2021-01-01        # Any valid date in 2021

site configuration

posts = '/posts/:year/:slug'

layout = 'by-year'
kind = 'section'
path = '/posts/**'


{{ define "main" }}
  <h1>{{ .Title }}</h1>
  {{ .Content }}

  {{ $p := where site.RegularPages "Type" "eq" .FirstSection.Type }}
  {{ $p = where $p "Date.Year" "eq" .CurrentSection.Date.Year }}
  {{ range $p }}
    <h2><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></h2>
  {{ end }}

{{ end }}

That worked! I didn’t even need to add any content in _index.md for it to start working, simply creating the files was enough for Hugo to render the page.

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