I am trying to write my own theme and do something that I thought would be very simple, but I cannot get it going after several days,
What I want is to classify my posts by subject at multiple levels e.g. Fiction / Detective, Fiction / Fantasy, History / Ancient.
Then I need:
a sidebar with the categories in hierarchical pages
when the user clicks on a category, a page with the list of the most recent n posts on that subject (linked to the individual posts)
it would be nice to have the choice of auto-generated or explicit page for this, but that is not essential
Could someone give me the actual text that I need to type into Hugo files for this.
ju52
November 25, 2018, 10:44am
2
I use this to sort my posts to series.
You can do this in frontmatter
subject = ["Fiction", "Fantasy", "Fiction-Fantasy"]
in the config you should add this
[taxonomies]
"subject" = "subjects"
[permalinks]
subject = "/:section/:title"
You can add the post do multiple subjects. For hierarchies you have to build a special list template.
zwbetz
November 25, 2018, 10:46am
3
Instead of using Sections to show relationships between your content, you could use a Category Taxonomy. This would make defining your Category “list” and “single” templates easier. Also, when adding another category, a user could just edit content front matter, instead of creating a new section.
Edit: looks like @ju52 beat me to it
I have come up with a test solution, so any comments would be good. This is just the bare bones of a solution.
Content file content/stuff/test1:
---
title: "Test1"
date: 2018-11-26T12:11:59+08:00
topics:
- subj2
---
This is a test.
Add some more similar files, Some should have topic subj1.
content/subjects/subj1:
---
title: "Subj1"
date: 2018-11-26T13:03:22+08:00
draft: false
menu:
main:
identifier: subj1
---
All about subject 1.
content/subjects/subj2:
---
title: "Subj2"
date: 2018-11-26T13:03:22+08:00
menu:
main:
parent: subj1
identifier: subj2
---
All about subject 2.
layouts/subjects/single.html (part only):
{{ $urlSplit := ( index (split .URL "/") 2) }}
{{ $tax := .Site.Taxonomies.topics }}
{{ $topic := (index $tax $urlSplit) }}
<ul>
{{ range $topic.Pages }}
<li><a href="{{ .URL }}">{{ .Name }} </a></li>
{{ end }}
</ul>
layouts/partials/sidebar.html:
<nav>
<ul>
<li><a href="/">Home</a>
{{ range .Site.Menus.main }}
<li>< a href="{{.URL}}"> {{ .Name }}</a></li>
{{if .HasChildren }}
<ul>
{{range .Children}}
<li><a href="{{.URL}}"> {{ .Name }}</a></li>
{{end}}
</ul>
{{end}}
{{end}}
</ul>
</nav>