Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show related posts #1049

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions assets/css/common/post-single.css
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,18 @@
margin-top: 56px;
}

.post-footer h3 {
margin: 2em 0 1.2em;
}

.post-footer .related ul {
padding-inline-start: 20px;
}

.post-footer .related a:hover {
box-shadow: 0 1px;
}

.post-tags li {
display: inline-block;
margin-inline-end: 3px;
Expand Down
3 changes: 3 additions & 0 deletions i18n/en.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,6 @@

- id: code_copied
translation: "copied!"

- id: related
translation: "Related posts"
3 changes: 3 additions & 0 deletions i18n/fr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,6 @@

- id: code_copied
translation: "Copié !"

- id: related
translation: "Sur le même sujet"
6 changes: 6 additions & 0 deletions layouts/_default/single.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ <h1 class="post-title">
{{- end }}

<footer class="post-footer">

{{/* Show related posts */}}
{{- if (.Param "ShowRelatedContent") }}
{{- partial "related.html" . }}
{{- end }}

{{- $tags := .Language.Params.Taxonomies.tag | default "tags" }}
<ul class="post-tags">
{{- range ($.GetTerms $tags) }}
Expand Down
13 changes: 13 additions & 0 deletions layouts/partials/related.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{{ $related := first 5 (where (where .Site.Pages ".Params.tags" "intersect" .Params.tags) "Permalink" "!=" .Permalink) }}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use Hugo's builtin "related posts" feature? https://gohugo.io/content-management/related/

Using it would be a lot more flexible and users would have a larger range of options to decide how relatedness is defined.

{{ with $related }}
<div class="related">
<h3 class="see-also">{{- i18n "related" -}}</h3>
<ul>
{{ range . }}
<li>
<a href="{{ .RelPermalink }}">{{ .Title }}</a>
</li>
{{ end }}
</ul>
</div>
{{ end }}