Display series titles with associated page links, but only if a member of a specific category

Some posts on my site are story chapters. They therefore have category ‘stories’ and are part of a series that is the name of the story. For example:

categories = ["Stories"]
series = ["Paternal-Bonds"]

Other posts are blog entries. Sometimes they are part of a series of blog posts and other times they are not. For example:

categories = ["Blog"]
series = ["Thought-Factory"]

If I wanted to display the story title, and the story posts associated with it, I would do something like this:

{{ range $key,$value := .Site.Taxonomies.series }}
<h4>{{ (title (humanize $key)) }}</h4>
{{ range $value.Pages.ByPublishDate.Reverse }}
<p><a href="{{ .Permalink }}">{{ .Title }}</a></p>
{{ end }}
{{ end }}

However, this also displays the blog posts which are part of a series. How can I limit it to just show the stories?

Thanks for any help.

With the above code you are only rendering posts from the series taxonomy. So it’s only natural that you are getting posts from both blog and stories categories.

But what you are trying to do is more complicated than it seems.

As far as I know Hugo taxonomies are not hierarchical, they exist side by side. You cannot do a simple check to render posts from a series taxonomy that also has a stories taxonomy.

A much simpler way to achieve what you want -in my humble opinion-, would be to organize your content by different /blog/ and /stories/ sections. This way you could easily get the posts you need from those sections with .GetPage and only render those that have a series front matter parameter.

Also it may very well be, that someone more skilled in Go templates than me, could offer you a magical solution without having to reorganize your content in different sections…

1 Like

Thanks for the response.

I feared that might be the case. I’ve done much the same thing by creating a second taxonomy term called ‘blogseries’ but I felt that was somewhat inelegant.

In any case, I’m glad to know it’s not a simple fix as it was starting to drive me crazy. At least I know I’m not being completely stupid :slight_smile:

Thanks again!

I used this approach. And Posts in the story can also have a category. Then I do something like this:

{{ $stories := where (where .Site.Pages "Section" "stories") ".Params.unlisted" "!=" true | intersect (where .Site.Pages "Params.story_published" "=" true)  }}

			{{ range $stories }} 
				{{ with .Params.story_subtitle }} <p>{{ . }}</p>{{ end }}
				<a href="{{ .Permalink }}"><h3>{{  .Params.story_title }}</h3></a>
				</article>
			{{ end }}
2 Likes

Thank you for sharing! :four_leaf_clover: