How to add a custom list page for nested directories

Consider a website with the following structure: website/writing/essays. This is also reflected in the ‘content’ directory (./content/writing/essays). The ‘writing’ and ‘essays’ pages are list pages. I have made a custom template in my theme for the ‘writing’ list page (./themes/website/layouts/writing/list.html). This template is used for both the ‘writing’ and ‘essays’ page. I’m assuming this is because the ‘essays’ page is a child of the ‘writing’ page. However, I’d like both to use a different list template.

I’ve tried this: ./themes/website/layouts/writing/essays/list.html. This doesn’t work. Does anyone have any suggestions on what might work?

You will need to add the custom template in the front matter of the essay folder’s _index.md file with layout: layout-name (and rename it to something like essay.html so the front matter will be layout: essay). The move the custom template to layouts/_default/.

Another way I do it on my site is to combine both layouts in the the same list.html as shown below. Then add list: essay to the _index.md file of the essay folder (called nested section in Hugo). Of course you can change list: and .Params.list to anything else you like.

{{ range .Pages }}
{{- if ne .Params.list "essay" }}
<--- list code goes here for writing -->
{{- else if eq .Params.list "essay" }}
<--- code for essay goes here -->
{{- end }}
{{- end }}
1 Like

Your first suggestion didn’t work for me. Essays’ _index.md has layout: essays and I’ve created layouts/_default/essays.html.

I’m now gonna try your second suggestion, which also seems cleaner!

Thanks for your reply.

Tried your second suggestion and it works, thank you.

It worked for me. So, perhaps there is something wrong with your layout/theme.
Ooh. I get it now. You also have a custom template for the writings section. Repeat the same step I recommended (first option), but put both the list.html and the essays.html files inside the /layouts/writing folder. That should work (add layout: essays to the essays _index.md file).

1 Like

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