Skip to content

Commit 90d3fbf

Browse files
rayjoltbep
authored andcommitted
tplimpl: Add translation links to the default sitemap template
For pages with translations, add links with hreflang attributes to the default sitemap template. This helps Google to show the correct language page in its search results. The syntax used is based on Google's example at [1]. Also update the sitemap template docs to reflect the changes in the default template. [1] https://support.google.com/webmasters/answer/2620865?hl=en&topic=2370587&ctx=topic Fixes #2569
1 parent 5f3ad1c commit 90d3fbf

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

‎docs/content/templates/sitemap.md‎

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,29 @@ one.
3131

3232
## Hugo’s sitemap.xml
3333

34-
This template respects the version 0.9 of the [Sitemap
35-
Protocol](http://www.sitemaps.org/protocol.html).
34+
This template uses the version 0.9 of the [Sitemap
35+
Protocol](http://www.sitemaps.org/protocol.html) with Google's [hreflang
36+
attributes](https://support.google.com/webmasters/answer/2620865?hl=en&topic=2370587&ctx=topic)
37+
for linking to translated content.
3638

37-
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
39+
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
40+
xmlns:xhtml="http://www.w3.org/1999/xhtml">
3841
{{ range .Data.Pages }}
3942
<url>
4043
<loc>{{ .Permalink }}</loc>{{ if not .Lastmod.IsZero }}
4144
<lastmod>{{ safeHTML ( .Lastmod.Format "2006-01-02T15:04:05-07:00" ) }}</lastmod>{{ end }}{{ with .Sitemap.ChangeFreq }}
4245
<changefreq>{{ . }}</changefreq>{{ end }}{{ if ge .Sitemap.Priority 0.0 }}
43-
<priority>{{ .Sitemap.Priority }}</priority>{{ end }}
46+
<priority>{{ .Sitemap.Priority }}</priority>{{ end }}{{ if .IsTranslated }}{{ range .Translations }}
47+
<xhtml:link
48+
rel="alternate"
49+
hreflang="{{ .Lang }}"
50+
href="{{ .Permalink }}"
51+
/>{{ end }}
52+
<xhtml:link
53+
rel="alternate"
54+
hreflang="{{ .Lang }}"
55+
href="{{ .Permalink }}"
56+
/>{{ end }}
4457
</url>
4558
{{ end }}
4659
</urlset>

‎tpl/tplimpl/template_embedded.go‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,24 @@ func (t *templateHandler) embedTemplates() {
8787
</channel>
8888
</rss>`)
8989

90-
t.addInternalTemplate("_default", "sitemap.xml", `<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
90+
t.addInternalTemplate("_default", "sitemap.xml", `<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
91+
xmlns:xhtml="http://www.w3.org/1999/xhtml">
9192
{{ range .Data.Pages }}
9293
<url>
9394
<loc>{{ .Permalink }}</loc>{{ if not .Lastmod.IsZero }}
9495
<lastmod>{{ safeHTML ( .Lastmod.Format "2006-01-02T15:04:05-07:00" ) }}</lastmod>{{ end }}{{ with .Sitemap.ChangeFreq }}
9596
<changefreq>{{ . }}</changefreq>{{ end }}{{ if ge .Sitemap.Priority 0.0 }}
96-
<priority>{{ .Sitemap.Priority }}</priority>{{ end }}
97+
<priority>{{ .Sitemap.Priority }}</priority>{{ end }}{{ if .IsTranslated }}{{ range .Translations }}
98+
<xhtml:link
99+
rel="alternate"
100+
hreflang="{{ .Lang }}"
101+
href="{{ .Permalink }}"
102+
/>{{ end }}
103+
<xhtml:link
104+
rel="alternate"
105+
hreflang="{{ .Lang }}"
106+
href="{{ .Permalink }}"
107+
/>{{ end }}
97108
</url>
98109
{{ end }}
99110
</urlset>`)

0 commit comments

Comments
 (0)