Post front matter content on index.html

Still learning the layers of the hugo template system. But hitting a few roadblocks.

Is it possible to pull in front matter from a post into a homepage template (index.html) in the root?


{{ if isset .Page.Params "credit"}}{{ }}, {{end}}{{ now.Year }}

On a single page, it renders the post credit from the front matter out.

But that doesn’t work on the homepage template, which is only pulling in 1 post with {{ range first 1 .Site.RegularPages }}, and in a list view for tags, I’d like it to say All content © their respected owners, 2020. So basically I need the copyright notice to change on the three different templates. I assume I need two else statements for that in the footer.html.

Any tips or pointing to the right documentation is appreciated. Thanks,

Can you show the full range code?

As long as you call inside it, it should work.

{{ range first 1 .Site.RegularPages }}
			<h2><a href="{{ .RelPermalink }}">{{ .Date.Format "Monday, January 2, 2006" }}</a></h2>
			{{ .Content }}
			<ul class="article-tags">
				{{ range .Params.tags }}
					<li><a href="{{ "tags/" | absURL }}{{ . | urlize}}">#{{ . }}</a></li>
				{{ end }}
		{{- partial "postnav.html" . -}}
		{{ end }}

I’m just pulling 1 post in on the homepage/index.html. But since the copyright is in the footer, brought into the homepage/index via baseof, I’m not seeing how to connect to frontmatter from the one post that’s brought in.

footer.html has

    <p>{{ if isset .Page.Params "credit"}}Content © {{ }}{{else}} Content © their respected owners, {{end}}{{ now.Year }}<br>
        Powered by <a target="_blank" href="">Hugo</a>, Theme <a target="_blank" href="">APAD</a>

visually, it may make more sense to see the footer changes in action on the different pages. Click previous at the bottom and you are taken to the previous post, switching from the homepage to the single page. List page if you click on a tag.

If I understood corrrectly, your index.html shows one post, but you want the credit to show up inside the footer.

Just add the range to the footer:

{{ range first 1 .Site.RegularPages }}
    <p>{{ if isset .Page.Params "credit"}}Content © {{ }}{{else}} Content © their respected owners, {{end}}{{ now.Year }}<br>
{{ end }}

Since the copyright is in the footer.html partial, it only can render the front matter from the single.html template. For the homepage/index.html and list.html templates, the range you wrote would work, but I’d have to have a second template or remove the footer from the baseof.html. Using the range renders the copyright from the first post only. even as you cycle through the posts.

I guess I’m asking if there is a way to do that all from one footer.html and return the correct front matter for index.html and single.html, and then have an else “All content ©” for a list page.

Sadly that doesn’t appear to work.

I keep trying to think of how to word this better, or explain it in a way that makes sense in terms of the site organization.

Is it possible to pull a posts frontmatter content from a single post into the index.html / homepage template?

My index.html/homepage template only retrieves the latest post using a range of 1.

I suppose I could do something kind of hackey, like kill my footer.html partial which contains the copyright code that works for single.html and bring in the footer with a single post as a different block or something.

can you share the full code somewhere?

Let’s see…I haven’t used git in like 10 years.

Thanks, I now understand your problem a lot better.

This should work for you:

	{{ if .IsHome }}
		{{ $currentPage := (where site.RegularPages "Section" "posts" | first 1) }}
		<p>Content © {{ range $currentPage }} {{}}{{end}}, {{ now.Year }}<br>
		    Powered by <a target="_blank" href="">Hugo</a>, Theme <a target="_blank" href="">APAD</a>
	{{ else }}
		{{ $currentPage := .Page }}
		<p>Content © {{ $currentPage }} {{}}, {{ now.Year }}<br>
		    Powered by <a target="_blank" href="">Hugo</a>, Theme <a target="_blank" href="">APAD</a>

	{{ end }}

Thanks! I can’t believe I didn’t see that solution yet.

I had to change {{ $currentPage }} {{}} to {{ }} for the else side of the statement. looks like it’s working now though.

1 Like