Skip to content

Instantly share code, notes, and snippets.

@jeremyjaymes
Last active March 21, 2021 08:39
Show Gist options
  • Select an option

  • Save jeremyjaymes/403f1cb712d98e8c8a36c904055958d6 to your computer and use it in GitHub Desktop.

Select an option

Save jeremyjaymes/403f1cb712d98e8c8a36c904055958d6 to your computer and use it in GitHub Desktop.
Hugo SEO Markup
<meta name="description" content="{{ if .IsHome }}{{ .Site.Params.description }}{{ else }}{{ .Description }}{{ end }}"/>
<meta name="robots" content="noodp"/>
<link rel="canonical" href="{{ .Permalink }}" />
<!-- Twitter Card -->
<meta name="twitter:card" content="summary" />
<meta name="twitter:description" content="{{ if .IsHome }}{{ .Site.Params.description }}{{ else }}{{ .Description }}{{ end }}" />
<meta name="twitter:title" content="{{ .Title }}{{ if .IsHome }} - {{ .Site.Params.Tagline }}{{ else }} - {{ .Site.Title }}{{ end }}" />
<meta name="twitter:site" content="{{ .Site.Params.twitter }}" />
<meta name="twitter:creator" content="{{ .Site.Params.twitter }}" />
<!-- OG data -->
<meta property="og:locale" content="en_US" />
<meta property="og:type" content="{{ if .IsPage }}article{{ else }}website{{ end }}" />
<meta content="{{ .Title }}{{ if .IsHome }} - {{ .Site.Params.Tagline }}{{ else }} - {{ .Site.Title }}{{ end }}" property="og:title">
<meta content="{{ if .IsHome }}{{ .Site.Params.description }}{{ else }}{{ .Description }}{{ end }}" property="og:description">
<meta property="og:url" content="{{ .Permalink }}" />
<meta property="og:site_name" content="{{ .Site.Title }}" />
{{ range .Params.categories }}<meta property="article:section" content="{{ . }}" />{{ end }}
{{ if isset .Params "date" }}<meta property="article:published_time" content="{{ time .Date }}" />{{ end }}
<!-- JSON-LD -->
{{ if .IsHome }}
<script type="application/ld+json">
{
"@context": "http://schema.org",
"@type": "WebSite",
"url": "{{ .Permalink }}",
"name": "{{ .Site.Title }}",
"author": {
"@type": "Person",
"name": "{{ .Site.Params.author }}"
},
"description": "{{ .Site.Params.description }}",
}
</script>
{{ end }}
{{ if .IsPage }}
<script type="application/ld+json">
{{if eq .Section "post"}}
{
"@context": "http://schema.org",
"@type": "BlogPosting",
"headline": "{{ .Title }}",
"genre": "{{ range .Params.categories }}{{ . }}{{ end }}",
"url": "{{ .Permalink }}",
"datePublished": "{{ time .Date }}",
"description": "{{ .Description }}",
"author": {
"@type": "Person",
"name": "{{ .Site.Params.author }}"
}
}
{{ else }}
{
"@context": "http://schema.org",
"@type": "WebSite",
"url": "{{ .Permalink }}",
"name": "{{ .Title }}",
"author": {
"@type": "Person",
"name": "{ .Site.Params.author }}"
},
"description": "{ .Site.Params.description }}",
}
{{ end }}
</script>
{{ end }}
@eddturtle
Copy link
Copy Markdown

Hi, this is great, but had a few issues:

  • Lines 32 + 34 expected a double '{' but only one found
  • script close on L66 I would have expected to be outside the inner if (so one line down)
    Thanks for sharing!

@jeremyjaymes
Copy link
Copy Markdown
Author

This was forever ago but wanted to say thanks for pointing out those few issues, oddly enough it compiled without errors at the time. Anyway, I sort of fell of the grid shortly after posting this which is why I'm only just correcting the Gist and responding now. Thanks!

@JeffTheMoreTech
Copy link
Copy Markdown

sorry, how to use it for hugo site?

@jeremyjaymes
Copy link
Copy Markdown
Author

I want to first point out that this is several years old so I can't guaranty it's validity. That said, it might help to have a look at it within the context of the theme I use it in - https://github.com/jeremyjaymes/jeremyjaymes.com/blob/master/themes/j/layouts/partials/seo.html

The template is used as a partial and called in the theme header, it then pulls in the necessary data depending on where you are within the site - e.g. a post page would output the relevant post data - title, publish date, etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment