Skip to content

Commit

Permalink
Improve code for list of related posts.
Browse files Browse the repository at this point in the history
Signed-off-by: Tekki <[email protected]>
  • Loading branch information
Tekki committed Mar 26, 2023
1 parent 5f41b65 commit f74f442
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 18 deletions.
3 changes: 2 additions & 1 deletion exampleSite/config/_default/params.toml
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ languageMenuName = "🌐"
# notice of the footer.
# since = 2016

# Show related content at the end of an article based on the 'series' taxonomy. Can be set in post front matter.
# Show related content at the end of an article or in the sidebar, based on the 'series' taxonomy.
# Both parameters can be overridden in the post's front matter.
# showRelatedInArticle = false
# showRelatedInSidebar = false

Expand Down
3 changes: 3 additions & 0 deletions exampleSite/content/post/bundle/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ categories:
tags:
- Hugo
series: Themes Guide
# the following overrides the settings from params.toml:
showRelatedInArticle: true
showRelatedInSidebar: true
---

[Page bundles](https://gohugo.io/content-management/page-bundles/) are an optional way to [organize page resources](https://gohugo.io/content-management/page-resources/) within Hugo.
Expand Down
8 changes: 4 additions & 4 deletions layouts/_default/single.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ <h2>{{ T "overview" }}</h2>
{{- .Content }}
</div>

{{- $showRelatedInArticle := true }}
{{- if eq $s.showRelatedInArticle false }}
{{- $showRelatedInArticle = false }}
{{- $showRelatedInArticle := $s.showRelatedInArticle }}
{{- if eq $p.showRelatedInArticle true }}
{{- $showRelatedInArticle = true }}
{{- else if eq $p.showRelatedInArticle false }}
{{- $showRelatedInArticle = false }}
{{- end }}
{{- if ne $showRelatedInArticle false }}
{{- if (and ($showRelatedInArticle) (isset $p "series")) }}
{{- partial "related" . }}
{{- end }}

Expand Down
12 changes: 5 additions & 7 deletions layouts/partials/related.html
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
{{ if isset .Params "series" }}
{{$related := where .Site.RegularPages ".Params.series" "eq" .Params.series }}
<h2 class="post_related">Posts in this Series</h2>
<ul>
{{ range $related }}
<li><a href="{{ .Permalink }}" class="nav-link" title="{{ .Title }}">{{ .Title }}</a></li>
{{ end }}
</ul>
<h2 class="post_related">{{ T "series_posts" }}</h2>
<ul>
{{ range $related }}
<li><a href="{{ .Permalink }}" class="nav-link" title="{{ .Title }}">{{ .Title }}</a></li>
{{ end }}
</ul>
14 changes: 8 additions & 6 deletions layouts/partials/sidebar.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,23 @@ <h2 class="mt-4">Disclaimer</h2>
</div>
{{ end }}

{{- $relatedInSidebar := true }}
{{- if eq $s.showRelatedInSidebar false }}
{{ $relatedInSidebar = false }}
{{- $showRelatedInSidebar := $s.showRelatedInSidebar }}
{{- if eq .Params.showRelatedInSidebar true }}
{{- $showRelatedInSidebar = true }}
{{- else if eq .Params.showRelatedInSidebar false }}
{{- $showRelatedInSidebar = false }}
{{- end }}
{{ if (and ($relatedInSidebar) (isset .Params "series") ) }}
{{- if (and ($showRelatedInSidebar) (isset .Params "series")) }}
{{$related := where .Site.RegularPages ".Params.series" "eq" .Params.series }}
<h2 class="mt-4">{{ T "series_posts" }}</h2>
<h2 class="mt-4">{{ T "series_posts" }}</h2>
<ul>
{{ range $related }}
<li>
<a href="{{ .Permalink }}" class="nav-link" title="{{ .Title }}">{{ .Title | markdownify }}</a>
</li>
{{ end }}
</ul>
{{ end }}
{{- end }}

{{- $posts := where .Site.RegularPages "Type" "in" $s.mainSections }}
{{- $featured := default 8 $s.numberOfFeaturedPosts }}
Expand Down

0 comments on commit f74f442

Please sign in to comment.