I’ve studied the docs and tried trail-and-error but remain stuck. Does someone know how to output a list of post from a specific category, ordered alphabetically?
Some things I’ve tried so far:
{{ range where $.Data.Pages $.Params.categories "example category" }}
{{ .Render "li" }}
{{ end }}
{{ range where .Data.Pages .Params.category "example-category" }}
{{ .Render "li" }}
{{ end }}
{{ range where $.Data.Pages $.Site.Taxonomies.categories "example-category" }}
{{ .Render "li" }}
{{ end }}
<ul>
{{ range .Site.Pages (where "categories" "example-category" )}}
<li>{{ .Title }}</li>
{{ end }}
</ul>
<ul>
{{ range where .Data.Pages ".Params.categories" "example-category" }}
<li>{{ .Title }}</li>
{{ end }}
</ul>
<ul>
{{ range .Site.Pages (where .Site.Taxonomies.categories "example-category") }}
<li>{{ .Title }}</li>
{{ end }}
</ul>
I got it partially working, this returns a list of all posts from a specific (sub)category:
{{ range .Site.Taxonomies.categories.categoryname }}
{{ if in .Page.Params.categories "Example category" }}
<li>Here's are the category posts: {{ .Page.Title }}</li>
{{ end }}
{{ end }}
The sorting is still where I’m stuck, this is how I approached that:
<!-- Get all posts from the category -->
{{ range .Site.Taxonomies.categories.categoryname }}
<!-- Sort them -->
{{ range sort .Page "Title" }}
<!-- Then only posts from the subcategory -->
{{ if in .Page.Params.categories "Example category"}}
<li>Sorted test: {{ .Page.Title }}</li>
{{ end }}
{{ end }}
{{ end }}
That gave me an error of
Error while rendering section *sectionname*: reflect: call of reflect.Value.Len on struct Value
I guess that’s from the Go reflect package. That suggests I need to use a Array, Chan, Map, Slice, or String somewhere. I just don’t know how or where.
I got it working quickly once I turned the problem around and didn’t use .Site.Taxonomies.categories.categoryname but first $.Site.Pages.ByTitle and only after that the filtering by categories.
This generates an alphabetically list of pages that belong to the “First category” and the “Second category” categories:
{{ range $.Site.Pages.ByTitle }}
{{ if and (in .Params.categories "First category") (in .Params.categories "Second category") }}
<li>{{ .Title }}</li>
{{ end }}
{{ end }}
For people who find this topic through the search: the ‘solution’ I found is not a real solution but has a strong negative impact on the performance of Hugo. See this topic for more.