Adding posts from a tag after the content of a single post

Hello,

I have a lot of short single posts,

i thought about filling single posts pages with several other posts (3 or 4à from the same tag (let’s say the first tag used by the current post).

currently to display the content of the single post i have

{{ .Render “fullcontent”}}

if i’m not wrong ;
how could i achieve my idea? help! :]

MM.

https://gohugo.io/content-management/related/

No-brainer.

1 Like

Wow thanks this works really well, and don’t even seem to add render time when deploying! Amazing!

I have one last question ; is it possible to desactivate it somehow on some posts?

You could add a front matter field, something like showRelated = false, and then wrap the “related pages” block with an if statement.

I added this code in my related.html partial:

{{ range where .Paginator.Pages “Params.relatedhidden” “ne” “true” }}
{{ .Render “fullcontent”}}
{{ end }}

i set the MetaDataFormat = “yaml” in my config.toml
so i used this in one of my post:

relatedhidden: true

i get this error message:

render of “page” failed: e[1;36m"/home/test/layouts/_default/single.html:37:3"e[0m: execute of template failed at <partial “related.html” .>: error calling partial: e[1;36m"/home/test/layouts/partials/related.html:6:27"e[0m: execute of template failed at <.Paginator.Pages>: can’t evaluate field Paginator in type page.Pages render: failed to render pages: can’t evaluate field Paginator in type page.Pages

https://gohugo.io/templates/pagination/#list-paginator-pages

This feature is currently only supported on homepage and list pages (i.e., taxonomies and section lists).

{{ $related := .Site.RegularPages.Related . | first 5 }}
{{ with $related }}
{{ range . “Params.relatedhidden” “ne” “true” }}
{{ .Render “fullcontent”}}
{{ end }}
{{ end }}

I’m trying to modify the related.html partial (quoted in this post) not to show on pages i set my relatedhidden variable to true

i don’t know what should i change the . to

{{ range . “Params.relatedhidden” “ne” “true” }}

to make it work like .Paginator.Pages works on sections and tag pages

First, when setting boolean parameters in front matter, do not quote them. If you do, they become strings instead of boolean values. So make sure it looks like this for TOML:

title = 'Something'
relatedhidden = true

Or this for YAML:

title: Something
relatedhidden: true

Second, to list the first five “not hidden” related pages:

{{ with site.RegularPages.Related . }}
  {{ range where . "Params.relatedhidden" "ne" true | first 5 }}
    {{ .Render "fullcontent" }}
  {{ end }}
{{ end }}
1 Like

This works really well, thank you!

I was also thinking about not rendering the related partial at all if another params would be set to true:

norelatedpartial: true

something i saw here: How to use partials for only certain pages - #2 by zwbetz

{{ if eq .Type “post” }}
{{- partial “some-partial.html” . -}}
{{ end }}

just modifying to something like:

{{ if . “Params.norelatedpartial” “ne” true }}
{{ partial “related.html” . }}
{{ end }

but i get errors:

render of “page” failed:/home/test/layouts/_default/single.html:37:6"e[0m: execute of template failed at <.>: can’t give argument to non-function . render: failed to render pages: can’t give argument to non-function .

if . "Params.norelatedpartial" "ne" true

It looks like you are trying to use syntax for the where function with an if statement. That’s not going to work.

{{ if not .Params.norelatedpartial }}
1 Like

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