List one authors' posts

This has to be simple :slight_smile: I have authors set up as a taxonomy in config.toml. I’m using the function below in a sidebar.html to list the five most recent posts by all authors:

 <ul class="recent-posts">
  {{- $recent_articles_num := (.Site.Params.widgets.recent_articles_num | default 5) }}
  {{- range first $recent_articles_num (where .Site.RegularPages "Section" "in" (.Site.Params.postSections | default (slice "posts"))) }}
     <li>
      <a href="{{ .RelPermalink }}">{{ .Title | truncate 50 }}</a>
      </li>
    {{- end }}
</ul>

How can I list the five most recent posts by one specific author?

How do call that one author in the range? Or is there a better way?

And what happens if a post has more than one author? Will that post show up in each authors’ listing?

Assuming you have

[taxonomies]
  author = "authors"

You can .GetPage the author page, something like:

{{ $author := site.GetPage "authors/arthur-conan-doyle" }}
{{ range first 5 $author.Pages }}
  {{ . }}
{{ end }}

See: https://gohugo.io/templates/taxonomy-templates/#sitegetpage-for-taxonomies

1 Like

Thanks! That works. I realized that I don’t need the .Site.Params.widgets.recent_articles_num in my first example; that was a holdover from a theme I modified.

Now I’m trying this: since an author can have posts in two different Sections, and I want to limit the output to “posts”, this is what I came up with, but it throws the error “wrong number of args for first: want 2 got 3.”

{{ $author := site.GetPage "authors/arthur-conan-doyle" }}
{{ range first 6 $author.Pages (where .Site.RegularPages ".Type" "posts" ) }}
<ul class="recent-posts">
<li>
<a href="{{ .RelPermalink }}">{{ .Title | truncate 50 }}</a>
</li>
</ul>
{{ end }}

You have to do something like:

{{ range first 6 (where $author.Pages ".Type" "posts" ) }}

Thanks!

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