How can I have a different frontmatter for my about page in Hugo?

752 views Asked by At

I'm using the PaperMod theme. I have the following params in my config.yml

---
# some predefined variables
params:
  ShowReadingTime: true
  ShowShareButtons: true
  ShowBreadCrumbs: true
  ShowCodeCopyButtons: true 
---

The params work as expected for my posts. But then I have single pages (e.g. About page) that I don’t want to use the same params. I tried overriding the above values in the index.md of my /about/ directory, but it didn't work.

So I read more in the docs and blog posts about _index.md, but not sure if I figured it out right, it seems that I should do something like this instead:

config.yml // remove such params
content 
  posts
     _index.md // only add params here 
     post1.md
     post2.md
  about.md // special page that doesn't need the params

But when I do such, the params I set don’t have any effect on post1, post2.

Am I doing this the right way? I thought I can think of content/posts as a section and each section would need a _index.md for its custom front matter variables.

2

There are 2 answers

2
Richard Smith On BEST ANSWER

I noticed that the theme uses the {{ .Param KEY }} function to obtain the value of these custom parameters.

The function first checks the page's parameters (front matter) then falls back to the global configuration file(s). In the global configuration file, custom parameters are specified under the params section, however, in the page's front matter, the custom parameters are specified at the root level.

2
Mr. Hugo On

I would set them in the config to make them site-wide variables. Then I would set different params in the page and overwrite with the logic below.

Referring to site-wide variables:

.Site.Params.yourparamkey

Referring to a page variable:

.Params.yourparamkey

Checking which one to use:

{{- if .Params.yourparamkey -}}
  {{- $yourparamkey := .Params.yourparamkey -}}
{{- else -}}
  {{- $yourparamkey := .Site.Params.yourparamkey -}}
{{- endif -}}

Using your param key:

{{ $yourparamkey }}