Going one step further than the answer here https://discourse.gohugo.io/t/list-one-authors-posts/29055 and using the function below to list a specific authors’ posts on an author’s bio page:
{{ $author := site.GetPage “authors/arthur-conan-doyle” }}
{{ range first 6 (where $author.Pages “.Type” “posts” ) }} {{ .Title | truncate 50 }}
{{- end }}
How can I dynamically get the author name from the permalink and use it something like authors/$current_author so that I can include the function above on any author template page without having to hardcode the author (i.e. “arthur-conan-doyle”) in template files?
Pull it from front matter, like you do with .Title - so the author name is set in the front-matter.
Your alternative is to get cute with string parsing on the url. . . but there are multiple potential problems with that - this is what front-matter is for.
Ah, I see. But "site.GetPage "authors/arthur-conan-doyle" is still the problem; I want the function to not require a hardcoded author slug like authors/arthur-conan-doyle. Can $authorPage be generated from Params.author?
Or, I could add each author slug as a Params in each _index.md, but that defeats the purpose.
Do I understand correctly that this is for a sidebar on something like an article/blog page? And the sidebar should list other articles by the same author?
This way will allow you to use have “Arthur Conan Doyle” in the content page’s author field, and on the /authors/arthur-content-doyle/_index.md page also have author field “Arthur Conan Doyle”, so this same author field is what serves as the link or common identifier between the pages.
{{ $authorName := .Params.author }}
{{ range where (.GetTerms "authors") "author" $authorName }}
Warning: I’ve not actually run this, so you’re going to have to adapt to work with your setup. I’m assuming the taxonomy page has a front-matter field “author” that contains the author name.
This is for an author’s bio page, not a sidebar or single, using the structure /content/authors/arthur-conan-doyle/_index.md and /layouts/author/lists.html. Your example doesn’t return anything; I’ll keep digging or use a Params for the slug.
do you mean that you’re on the author’s taxonomy page /content/authors/arthur-conan-doyle/_index.md and you just want to list all the pages belonging to that taxonomy term (the author, in this case)?
Thanks! And yes, we were overthinking it, and the answer to my original question here https://discourse.gohugo.io/t/list-one-authors-posts/29055 is more complex than need be.
Your version {{ range first 6 (where .Pages ".Type" "posts" ) }}
works on /layouts/author/lists.html.
And for use in a sidebar, this works: {{ range first 6 (where .Site.RegularPages ".Type" "posts" ) }}