# How can I get the author from the permalink to use as a variable?

**URL:** <https://discourse.gohugo.io/t/how-can-i-get-the-author-from-the-permalink-to-use-as-a-variable/29083>\
**Category:** support\
**Created:** [October 28, 2020, 2:41am UTC](https://discourse.gohugo.io/t/how-can-i-get-the-author-from-the-permalink-to-use-as-a-variable/29083 "2020-10-28T02:41:46Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![bluedogranch](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bluedogranch/32/10538_2.png) [@bluedogranch](https://discourse.gohugo.io/u/bluedogranch)\
**Post date:** [October 28, 2020, 2:41am UTC](https://discourse.gohugo.io/t/how-can-i-get-the-author-from-the-permalink-to-use-as-a-variable/29083/1 "2020-10-28T02:41:46Z")

</div>

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:

```
config.toml:
[taxonomies] author = "authors"

```

> /content/authors/arthur-conan-doyle/\_index.md contains:  
> author: "Arthur Conan Doyle

{{ $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?

---

<div class="post-metadata">

**Author:** ![yaythomas](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/yaythomas/32/12334_2.png) [@yaythomas](https://discourse.gohugo.io/u/yaythomas)\
**Post date:** [October 28, 2020, 10:08am UTC](https://discourse.gohugo.io/t/how-can-i-get-the-author-from-the-permalink-to-use-as-a-variable/29083/2 "2020-10-28T10:08:03Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![bluedogranch](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bluedogranch/32/10538_2.png) [@bluedogranch](https://discourse.gohugo.io/u/bluedogranch)\
**Post date:** [October 28, 2020, 2:42pm UTC](https://discourse.gohugo.io/t/how-can-i-get-the-author-from-the-permalink-to-use-as-a-variable/29083/3 "2020-10-28T14:42:34Z")

</div>

That’s a good idea, as `{{ .Params.author }}` already works to get the dynamic page title from /content/authors/my-author/\_index.md`.

But how would I pull the author into a page listing function? Using `$author := .Params.author` obviously doesn’t work:

```
{{ $author := .Params.author }}
	{{ range first 6 (where $author.Pages ".Type" "posts" ) }}
       <li>
        <a href="{{ .RelPermalink }}">{{ .Title | truncate 50 }}</a>
       </li>
 {{- end }}
```

---

<div class="post-metadata">

**Author:** ![yaythomas](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/yaythomas/32/12334_2.png) [@yaythomas](https://discourse.gohugo.io/u/yaythomas)\
**Post date:** [October 28, 2020, 4:48pm UTC](https://discourse.gohugo.io/t/how-can-i-get-the-author-from-the-permalink-to-use-as-a-variable/29083/4 "2020-10-28T16:48:00Z")

</div>

You’re almost there. . .

Remember when you get the author name this is a title, it’s not actually the collection of pages you want to range through.

```
{{ $authorPage := site.GetPage "authors/arthur-conan-doyle" }}
{{ $authorName := $authorPage.Params.author }}
{{ range first 6 (where $authorPage.Pages ".Type" "posts" ) }}
       <a href="{{ .RelPermalink }}">{{ .Title | truncate 50 }}</a>
{{- end }}
```

---

<div class="post-metadata">

**Author:** ![bluedogranch](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bluedogranch/32/10538_2.png) [@bluedogranch](https://discourse.gohugo.io/u/bluedogranch)\
**Post date:** [October 28, 2020, 5:27pm UTC](https://discourse.gohugo.io/t/how-can-i-get-the-author-from-the-permalink-to-use-as-a-variable/29083/5 "2020-10-28T17:27:28Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![yaythomas](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/yaythomas/32/12334_2.png) [@yaythomas](https://discourse.gohugo.io/u/yaythomas)\
**Post date:** [October 28, 2020, 7:30pm UTC](https://discourse.gohugo.io/t/how-can-i-get-the-author-from-the-permalink-to-use-as-a-variable/29083/6 "2020-10-28T19:30:21Z")

</div>

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?

Check out the docs here for something very similar: [https://gohugo.io/templates/taxonomy-templates/#example-comma-delimit-tags-in-a-single-page-template](https://gohugo.io/templates/taxonomy-templates/#example-comma-delimit-tags-in-a-single-page-template).

This example assumes your content page has front-matter field “author”, which going by your example would be set to “arthur-conan-doyle”

```
    {{ $taxo := "authors" }}
    {{ $authorName := .Params.author }}
    {{ $authorPage := $.Site.GetPage (printf "/%s/%s" $taxo $authorName) -}}
    {{ range first 6 (where $authorPage.Pages ".Type" "posts" ) }}
      <li>
        <a href="{{ .RelPermalink }}">{{ .Title | truncate 50 }}</a>
      </li>
    {{- end -}}

```

All of that being said, the .GetPage construction is relatively brittle, I would maybe look into using `.GetTerms` with a `where` clause to get at the taxonomy term data you want. It’s documented here: [https://gohugo.io/templates/taxonomy-templates/#example-list-tags-in-a-single-page-template](https://gohugo.io/templates/taxonomy-templates/#example-list-tags-in-a-single-page-template) and [https://gohugo.io/functions/where/](https://gohugo.io/functions/where/)

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.

```auto
{{ $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.

---

<div class="post-metadata">

**Author:** ![bluedogranch](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bluedogranch/32/10538_2.png) [@bluedogranch](https://discourse.gohugo.io/u/bluedogranch)\
**Post date:** [October 28, 2020, 8:12pm UTC](https://discourse.gohugo.io/t/how-can-i-get-the-author-from-the-permalink-to-use-as-a-variable/29083/7 "2020-10-28T20:12:50Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![yaythomas](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/yaythomas/32/12334_2.png) [@yaythomas](https://discourse.gohugo.io/u/yaythomas)\
**Post date:** [October 28, 2020, 8:33pm UTC](https://discourse.gohugo.io/t/how-can-i-get-the-author-from-the-permalink-to-use-as-a-variable/29083/8 "2020-10-28T20:33:38Z")

</div>

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)?

if this is the case we’ve massively been over-complicating things - just use the standard taxonomy list template. [https://gohugo.io/templates/lists/#taxonomy-template](https://gohugo.io/templates/lists/#taxonomy-template)

So in `/layouts/author/lists.html`:

```
<h1>{{ .Title }}</h1>
<ul>
{{ range first 6 (where .Pages ".Type" "posts" ) }}
  <li>
    <a href="{{ .RelPermalink }}">{{ .Title | truncate 50 }}</a>
  </li>
{{ end }}
</ul>

```

In this case `.Title` is whatever the page title is - which is likely to be your author name.

---

<div class="post-metadata">

**Author:** ![bluedogranch](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bluedogranch/32/10538_2.png) [@bluedogranch](https://discourse.gohugo.io/u/bluedogranch)\
**Post date:** [October 31, 2020, 4:57pm UTC](https://discourse.gohugo.io/t/how-can-i-get-the-author-from-the-permalink-to-use-as-a-variable/29083/9 "2020-10-31T16:57:56Z")

</div>

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" ) }}`

---

<div class="post-metadata">

**Author:** ![system](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/system/32/1_2.png) [@system](https://discourse.gohugo.io/u/system)\
**Post date:** [November 2, 2020, 4:58pm UTC](https://discourse.gohugo.io/t/how-can-i-get-the-author-from-the-permalink-to-use-as-a-variable/29083/10 "2020-11-02T16:58:02Z")

</div>

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