# How to get permalink of Taxonomies in templates?

**URL:** <https://discourse.gohugo.io/t/how-to-get-permalink-of-taxonomies-in-templates/12927>\
**Category:** support\
**Created:** [July 13, 2018, 11:16pm UTC](https://discourse.gohugo.io/t/how-to-get-permalink-of-taxonomies-in-templates/12927 "2018-07-13T23:16:59Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![shaform](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/shaform/32/6419_2.png) [@shaform](https://discourse.gohugo.io/u/shaform)\
**Post date:** [July 13, 2018, 11:16pm UTC](https://discourse.gohugo.io/t/how-to-get-permalink-of-taxonomies-in-templates/12927/1 "2018-07-13T23:16:59Z")

</div>

We can set permalink for taxonomy as follows in config.toml:

```
[permalinks]
tags = "/tag/:slug/"

```

So that instead of `/tags/TAG_NAME`, we get `/tag/TAG_NAME` as the URL of a given tag.

However, some templates hardcoded the link to a tag page with

```
{{ range .Params.tags }}  
<a href="{{ "/tags/" | relLangURL }}{{ . | urlize }}">{{ . }}</a>
{{ end }}

```

Therefore, it always points to `/tags/TAG_NAME` no matter what is the setting for

```
[permalinks]

```

This was pointed out here as well:

[https://github.com/gohugoio/hugo/issues/4130](https://github.com/gohugoio/hugo/issues/4130) .

Are there any ways to retrieve the permalink of a given taxonomy in templates?

Thank you.

---

<div class="post-metadata">

**Author:** ![maiki](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/maiki/32/16384_2.png) [@maiki](https://discourse.gohugo.io/u/maiki)\
**Post date:** [July 13, 2018, 11:59pm UTC](https://discourse.gohugo.io/t/how-to-get-permalink-of-taxonomies-in-templates/12927/2 "2018-07-13T23:59:45Z")

</div>

Do you have a sample project that shows your issue? If you link to something I can clone, I’ll take a look.

In the meantime, check the [example for listing tags in a single page template](https://gohugo.io/templates/taxonomy-templates/#example-list-tags-in-a-single-page-template), which applies [urlize](https://gohugo.io/functions/urlize/), and may solve the issue you are having (it isn’t clear to me what the issue is, as the discussion linked to didn’t have a resolution).

---

<div class="post-metadata">

**Author:** ![shaform](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/shaform/32/6419_2.png) [@shaform](https://discourse.gohugo.io/u/shaform)\
**Post date:** [July 14, 2018, 12:03am UTC](https://discourse.gohugo.io/t/how-to-get-permalink-of-taxonomies-in-templates/12927/3 "2018-07-14T00:03:43Z")

</div>

Hi maiki,

If we use this example

```
<ul id="tags">
  {{ range .Params.tags }}
    <li><a href="{{ "/tags/" | relLangURL }}{{ . | urlize }}">{{ . }}</a> </li>
  {{ end }}
</ul>

```

The link to a given tag would be `/tags/TAG_NAME`.

But actually because the setting of permalink, the page would not be found (ERROR 404), because the actual path of the page is `/tag/TAG_NAME`.

But it’s not okay to hardcode `tag` into templates either, because the `permalink` setting could differ site by site. For example, using the following can solve this problem but it breaks sites that do not change the `permalink`:

```
<ul id="tags">
  {{ range .Params.tags }}
    <li><a href="{{ "/tag/" | relLangURL }}{{ . | urlize }}">{{ . }}</a> </li>
  {{ end }}
</ul>
```

---

<div class="post-metadata">

**Author:** ![shaform](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/shaform/32/6419_2.png) [@shaform](https://discourse.gohugo.io/u/shaform)\
**Post date:** [July 14, 2018, 12:05am UTC](https://discourse.gohugo.io/t/how-to-get-permalink-of-taxonomies-in-templates/12927/4 "2018-07-14T00:05:27Z")

</div>

BTW, the ability to change the permalink of tag pages was added in this PR: [https://github.com/gohugoio/hugo/pull/4061](https://github.com/gohugoio/hugo/pull/4061) .

---

<div class="post-metadata">

**Author:** ![maiki](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/maiki/32/16384_2.png) [@maiki](https://discourse.gohugo.io/u/maiki)\
**Post date:** [July 14, 2018, 12:26am UTC](https://discourse.gohugo.io/t/how-to-get-permalink-of-taxonomies-in-templates/12927/5 "2018-07-14T00:26:13Z")

</div>

> [@shaform](#):
>
> But it’s not okay to hardcode `tag` into templates either, because the `permalink` setting could differ site by site.

I disagree with your assertion. 🙂 It is okay to put the taxonomy patterns your site uses in the templates. Some folks grab themes specifically to use the taxonomies they set up.

Do you have a specific reservation about adding your site’s taxonomies to the `single.html` template? The general use case is set and forget, meaning when you set up the taxonomies, they don’t change.

Because we use Go templates, we switch contexts when building, and from what I understand, that is why we don’t just pull up a list of URLs and instead we build them as templated, “hardcoded” values. I hope someone knows how to do what you want, though! 🙂

---

<div class="post-metadata">

**Author:** ![shaform](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/shaform/32/6419_2.png) [@shaform](https://discourse.gohugo.io/u/shaform)\
**Post date:** [July 14, 2018, 1:41am UTC](https://discourse.gohugo.io/t/how-to-get-permalink-of-taxonomies-in-templates/12927/6 "2018-07-14T01:41:14Z")

</div>

Thanks for the reply. I do agree that the hardcoded path might be okay in certain situations. But I would also like to give some reasoning about why I think it’s better to remove the hardcoded path.

1. It’s actually possible to have different URLs for different languages:

2. It’s easy to change `permalinks` for other sections such as `post` without modifying the theme. It seems preferable to have the same ability for `tags`.

---

<div class="post-metadata">

**Author:** ![alexandros](https://avatars.discourse-cdn.com/v4/letter/a/ecc23a/32.png) [@alexandros](https://discourse.gohugo.io/u/alexandros)\
**Post date:** [July 14, 2018, 10:00am UTC](https://discourse.gohugo.io/t/how-to-get-permalink-of-taxonomies-in-templates/12927/7 "2018-07-14T10:00:52Z")

</div>

I think that you need to open a GitHub issue for this.

From what I gather, you are asking about something like a variable for custom taxonomy permalinks configuration. Something like `{{ .TaxonomyPermalink }}` or even making these available through  
`{{ .Permalink }}`.

It’s better to ask the Devs directly.

---

<div class="post-metadata">

**Author:** ![shaform](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/shaform/32/6419_2.png) [@shaform](https://discourse.gohugo.io/u/shaform)\
**Post date:** [July 14, 2018, 10:57am UTC](https://discourse.gohugo.io/t/how-to-get-permalink-of-taxonomies-in-templates/12927/8 "2018-07-14T10:57:09Z")

</div>

Thanks! I will.  
I am asking here first because in issue [https://github.com/gohugoio/hugo/issues/4130](https://github.com/gohugoio/hugo/issues/4130), @bep seems to indicate that doing so might already be possible.  
Suppose this is not the case, and the issue is not reopened by others, I will open a new issue for this.

---

<div class="post-metadata">

**Author:** ![vassudanagunta](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/vassudanagunta/32/4113_2.png) [@vassudanagunta](https://discourse.gohugo.io/u/vassudanagunta)\
**Post date:** [July 16, 2018, 10:39pm UTC](https://discourse.gohugo.io/t/how-to-get-permalink-of-taxonomies-in-templates/12927/9 "2018-07-16T22:39:19Z")

</div>

If [Pull Request #4796](https://github.com/gohugoio/hugo/pull/4796) is merged, then you won’t need to hardcode anymore. You could use

```auto
{{% relref "tags" %}}

```

which would be resolved to whatever url `tags` is published to. Likewise a call to `.GetPage "tags"` would return you the page object, from which you could query its actual URL, title or whatever.

---

<div class="post-metadata">

**Author:** ![kaushalmodi](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/kaushalmodi/32/2567_2.png) [@kaushalmodi](https://discourse.gohugo.io/u/kaushalmodi)\
**Post date:** [July 16, 2018, 10:53pm UTC](https://discourse.gohugo.io/t/how-to-get-permalink-of-taxonomies-in-templates/12927/10 "2018-07-16T22:53:52Z")

</div>

There is no need to hard-code the path for “tags” page or any taxonomy term page for that matter.

See [this](https://github.com/kaushalmodi/hugo-bare-min-theme/blob/master/layouts/_default/baseof.html) for example (Search for **taxonomyTerm** in that code). See the categories and tags links created at the top of the page [here](https://ox-hugo.scripter.co/test/) using that template.

---

<div class="post-metadata">

**Author:** ![shaform](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/shaform/32/6419_2.png) [@shaform](https://discourse.gohugo.io/u/shaform)\
**Post date:** [July 17, 2018, 12:00am UTC](https://discourse.gohugo.io/t/how-to-get-permalink-of-taxonomies-in-templates/12927/11 "2018-07-17T00:00:18Z")

</div>

@kaushalmodi: When the permalink is modified, the URL structure of the “tag list” page and “individual tag” page could be different. For example, when we set:

```
 [permalinks]
   tags = "/tag/:slug/"

```

The “tag list” page would still be `/tags/`, while the “individual tag” page is `/tag/TAG_NAME/`.  
While your example handles the “tag list” page, it does not seem to address the “individual tag” page.

@vassudanagunta: Does the PR handles the difference described above as well?

---

<div class="post-metadata">

**Author:** ![kaushalmodi](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/kaushalmodi/32/2567_2.png) [@kaushalmodi](https://discourse.gohugo.io/u/kaushalmodi)\
**Post date:** [July 17, 2018, 12:14am UTC](https://discourse.gohugo.io/t/how-to-get-permalink-of-taxonomies-in-templates/12927/12 "2018-07-17T00:14:35Z")

</div>

> [@shaform](#):
>
> While your example handles the “tag list” page, it does not seem to address the “individual tag” page.

The “individual tag” case is possible. See:

- [layouts/\_default/terms.html · master · Kaushal Modi / hugo-theme-refined · GitLab](https://gitlab.com/kaushalmodi/hugo-theme-refined/blob/master/layouts/_default/terms.html)
- [Tags ❚ A Scripter's Notes](https://scripter.co/tags/)

---

<div class="post-metadata">

**Author:** ![shaform](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/shaform/32/6419_2.png) [@shaform](https://discourse.gohugo.io/u/shaform)\
**Post date:** [July 17, 2018, 2:27am UTC](https://discourse.gohugo.io/t/how-to-get-permalink-of-taxonomies-in-templates/12927/13 "2018-07-17T02:27:10Z")

</div>

Thank you very much! That’s exactly what I need.

---

<div class="post-metadata">

**Author:** ![larsks](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/larsks/32/8554_2.png) [@larsks](https://discourse.gohugo.io/u/larsks)\
**Post date:** [May 15, 2019, 3:28pm UTC](https://discourse.gohugo.io/t/how-to-get-permalink-of-taxonomies-in-templates/12927/14 "2019-05-15T15:28:00Z")

</div>

I was trying to do the same thing described in this question, and I’m having a hard time following the answers. Using the [ghostwriter](https://themes.gohugo.io/ghostwriter/) theme as an example, we see this in `layouts/partial/post-footer.html`:

```auto
    {{ if .Params.tags }}                                                       
        <p class="post-tags">                                                   
            <span>Tagged:</span>                                                
            {{ $len := len .Params.tags }}                                      
            {{ range $index, $tag := .Params.tags }}                            
                <a href="{{ "/tags/" | relLangURL }}{{ . | urlize }}/">{{ . }}</a>
                {{- if lt $index (sub $len 1) -}}, {{ end }}                    
            {{ end }}                                                           
        </p>                                                                    
    {{ end}}                                                                    

```

It hardcodes the path to tag pages as `/tags/`. I would like this to automatically select a path that is consistent with the tag permalink settings (using `/tags/` or `/tag/` or `/bob/` or whatever as appropriate). How would modify the template to do this?

---

<div class="post-metadata">

**Author:** ![kaushalmodi](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/kaushalmodi/32/2567_2.png) [@kaushalmodi](https://discourse.gohugo.io/u/kaushalmodi)\
**Post date:** [May 15, 2019, 4:14pm UTC](https://discourse.gohugo.io/t/how-to-get-permalink-of-taxonomies-in-templates/12927/15 "2019-05-15T16:14:57Z")

</div>

In the link I shared earlier, look at [this line](https://gitlab.com/kaushalmodi/hugo-theme-refined/blob/e0a6e87f245211317f2fcd13a44608fc3ebf9224/layouts/_default/terms.html#L9). Using that, I can use that `terms.html` tempalte for _any_ taxonomy term… the `.Type` returns `"tags"`, "`categories"`, whatever.

… and then see how I use that `$type` in the `site.GetPage` in [this line](https://gitlab.com/kaushalmodi/hugo-theme-refined/blob/e0a6e87f245211317f2fcd13a44608fc3ebf9224/layouts/_default/terms.html#L16).

---

<div class="post-metadata">

**Author:** ![larsks](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/larsks/32/8554_2.png) [@larsks](https://discourse.gohugo.io/u/larsks)\
**Post date:** [May 15, 2019, 5:00pm UTC](https://discourse.gohugo.io/t/how-to-get-permalink-of-taxonomies-in-templates/12927/16 "2019-05-15T17:00:04Z")

</div>

So in a post template, I would use something like this?

```auto
{{ range $index, $tag := .Params.tags }}
  {{ $term := . }}
  {{ with .Site.GetPage (printf "/%s/%s" "tags" $term) }}
    <a href="{{ .Permalink }}">{{ $term }}</a>
    {{- if lt $index (sub $len 1) -}}, {{ end }}
  {{ end }}
{{ end }}

```

That results in:

```auto
executing "partials/post-footer.html" at <.Site.GetPage>: can't evaluate field Site in type string

```

---

<div class="post-metadata">

**Author:** ![larsks](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/larsks/32/8554_2.png) [@larsks](https://discourse.gohugo.io/u/larsks)\
**Post date:** [May 15, 2019, 5:05pm UTC](https://discourse.gohugo.io/t/how-to-get-permalink-of-taxonomies-in-templates/12927/17 "2019-05-15T17:05:02Z")

</div>

It looks like I need `$.Site.GetPage` instead. With that change, it seems to work. Thanks!

---

<div class="post-metadata">

**Author:** ![kaushalmodi](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/kaushalmodi/32/2567_2.png) [@kaushalmodi](https://discourse.gohugo.io/u/kaushalmodi)\
**Post date:** [May 15, 2019, 5:05pm UTC](https://discourse.gohugo.io/t/how-to-get-permalink-of-taxonomies-in-templates/12927/18 "2019-05-15T17:05:37Z")

</div>

> [@larsks](#):
>
> It looks like I need `$.Site.GetPage` instead

You are probably on an old Hugo version; I’d suggest updating to the latest release.

---

<div class="post-metadata">

**Author:** ![larsks](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/larsks/32/8554_2.png) [@larsks](https://discourse.gohugo.io/u/larsks)\
**Post date:** [May 15, 2019, 5:06pm UTC](https://discourse.gohugo.io/t/how-to-get-permalink-of-taxonomies-in-templates/12927/19 "2019-05-15T17:06:18Z")

</div>

I am using 0.55.5, which I believe is current.

---

<div class="post-metadata">

**Author:** ![kaushalmodi](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/kaushalmodi/32/2567_2.png) [@kaushalmodi](https://discourse.gohugo.io/u/kaushalmodi)\
**Post date:** [May 15, 2019, 5:07pm UTC](https://discourse.gohugo.io/t/how-to-get-permalink-of-taxonomies-in-templates/12927/20 "2019-05-15T17:07:34Z")

</div>

OK, I misunderstood; I thought you had issue using `site. ..`. In that case, just replace `.Site.foo` and `$.Site.foo` with `site.foo` everywhere.

[Next page](https://discourse.gohugo.io/t/how-to-get-permalink-of-taxonomies-in-templates/12927.md?page=2)
