Hi,
I’m trying to figure out how I should solve this problem. I’m using Hugo v0.29.
The basic idea is this:
“If visitor accesses a page from a category, I want the URL to be based on that category, so I can show similar pages for that category”.
This does break the idea of non-duplicate content, but, I would then use .Permalink
as the link rel="canonical"
(ref: hxxps://support.google.com/webmasters/answer/139066?hl=en) to specify that the post, independently of what tag you accessed it from, has one real link that is the correct permalink.
At the same time, I wanted to remove the word categories
from the URL, by just having the category directly.
I’ve been able to get to this point:
[permalinks]
category = "/:filename"
(this puts all categories in the root, without the keyword category
)
Then I set:
pagecat: "shirts"
In category/shirts/index.md
Then, on the category page, I loop out the pages like this:
{{ $pagecat := .Params.pagecat }}
{{ range $name, $taxonomy := .Site.Taxonomies.categories }}
{{ if eq $name $pagecat }}
{{ range $taxonomy.Pages }}
<li><a href="{{ "" | relLangURL }}{{ $name | urlize }}/{{ .Params.Slug }}">{{ .Title }} {{ .Params.Price }}</a></li>
{{ end }}
{{ end }}
{{ end }}
(This is using the technique explained here: hxxps://novelist.xyz/tech/custom-urls-for-category-pages-in-hugo/ )
As you see above, I’m not using the .Permalink
, but instead building up the URL myself using the taxonomy name and the .Slug
.
However, here’s the problem, I’m not able to regenerate versions for the same page under each taxonomy category so I can loop different items related to that category. I had an idea to hack this using alias.html
that was implemented a while ago (hxxps://discourse.gohugo.io/t/customize-alias-page/4242/4) but the problem is that the alias.html
-file is not specific to a section, and also it does not seem to be able to access the Page-attributes any more:
alias.html
:
Test: {{ .Page.Params.Title }}
Gives:
Error: Error building site: template: alias.html:1:14: executing "alias.html" at <.Page.Params.title>: can't evaluate field Params in type *hugolib.Page
When buildling. Also, it seems like no test is actually verifying that the Page.Params
can actually be accessed: https://github.com/gohugoio/hugo/blob/v0.29/hugolib/alias_test.go#L41 so this might actually be a bug that wasn’t spotted by the tests.
If alias.html
would work, I could use that one as the non-Permalink
-version of the page, having the link rel="canonical"
header but the same content as the real page, which would solve this issue. The only problem then would be that the alias.html
is not section dependent, but site-wide, but that might be something that would be possible to solve (maybe even I could do it).
So, question:
Is there any good way to solve the problem with different related information on the article, depending on from what taxonomy category you accessed it with by making copies of the page but being rendered for different taxonomies depending on the article? (I rather not fix this with javascript, and duplicate content is not an issue by using the link rel="canonical"
)
To understand my issue, I’ve attached a test-project (https://www.dropbox.com/s/b5u5iyqm4hdzeaz/test-project.zip?dl=1) that won’t even start due to the alias-error. When removing the {{ .Page.Params.Title }}
in alias.html
you will get it to build.
Would love to know how others have solved the issue with “different related content for the same article in different categories” before.
(Sorry for the garbled links, but new users wasn’t allowed to post more than two links in a post)
Regards,
Frans