Adding Frequently Asked Questions (FAQs) and FAQ Schema for Hugo - beginners edition—
- Add the code below in your
single.html
file inside the code that wraps your content ({{ .Content }}
). e.g inside the opening<article>
tag.
{{ if $faq }} itemscope itemtype="https://schema.org/FAQPage" {{ end }}
Which would look like this–
<article {{ if $faq }} itemscope itemtype="https://schema.org/FAQPage" {{ end }}>
{{ .Content }}
</article>
- Create an
faq.html
file insidelayouts/shortcodes
(base layouts folder or theme’s) and paste the code below in it
<div itemscope itemtype="https://schema.org/Question" itemprop="mainEntity">
<h2 itemprop="name">{{ with .Get "title" }}{{ . }}{{ end }}</h2>
<div itemscope itemtype="https://schema.org/Answer" itemprop="acceptedAnswer">
<div itemprop="text">{{ .Inner | markdownify }}</div>
</div>
</div>
Change the h2
value to whatever pleases you or your content structure, if you don’t want the question to be a second level heading.
3. Add faq: true
to your page’s front matter that you want to contain the FAQs, for example–
---
title: Hugo is Awesome
slug: "awesome-hugo"
faq: true
---
- Use the shortcode in your FAQ page as follows–
{{< faq title="<!-- insert the question here -->" >}}
<!-- insert the answer here. Supports paragraphs, lists, etc.).
{{< /faq >}}
Validate your schema with Google’s structured data testing tools.