mirror of
https://github.com/tiennm99/hugoDocs.git
synced 2026-10-14 18:18:37 +00:00
1 parent
e0d08cdbbc
commit
9a3675aad2
1 file changed
+58
-77
@@ -1,10 +1,7 @@
|
|||||||
---
|
---
|
||||||
title: Sitemap Template
|
title: Sitemap Templates
|
||||||
# linktitle: Sitemap
|
description: Hugo provides built-in sitemap templates.
|
||||||
description: Hugo ships with a built-in template file observing the v0.9 of the Sitemap Protocol, but you can override this template if needed.
|
|
||||||
date: 2017-02-01
|
date: 2017-02-01
|
||||||
publishdate: 2017-02-01
|
|
||||||
lastmod: 2017-02-01
|
|
||||||
categories: [templates]
|
categories: [templates]
|
||||||
keywords: [sitemap, xml, templates]
|
keywords: [sitemap, xml, templates]
|
||||||
menu:
|
menu:
|
||||||
@@ -15,92 +12,76 @@ weight: 160
|
|||||||
sections_weight: 160
|
sections_weight: 160
|
||||||
draft: false
|
draft: false
|
||||||
aliases: [/layout/sitemap/,/templates/sitemap/]
|
aliases: [/layout/sitemap/,/templates/sitemap/]
|
||||||
toc: false
|
toc: true
|
||||||
---
|
---
|
||||||
|
|
||||||
A single Sitemap template is used to generate the `sitemap.xml` file.
|
## Overview
|
||||||
Hugo automatically comes with this template file. *No work is needed on
|
|
||||||
the users' part unless they want to customize `sitemap.xml`.*
|
|
||||||
|
|
||||||
A sitemap is a `Page` and therefore has all the [page variables][pagevars] available to use in this template along with Sitemap-specific ones:
|
Hugo's built-in sitemap templates conform to v0.9 of the [sitemap protocol].
|
||||||
|
|
||||||
`.Sitemap.ChangeFreq`
|
With a monolingual project, Hugo generates a sitemap.xml file in the root of the [`publishDir`] using the internal [sitemap.xml] template.
|
||||||
: The page change frequency
|
|
||||||
|
|
||||||
`.Sitemap.Priority`
|
With a multilingual project, Hugo generates:
|
||||||
: The priority of the page
|
|
||||||
|
|
||||||
`.Sitemap.Filename`
|
- A sitemap.xml file in the root of each site (language) using the built-in [sitemap.xml] template
|
||||||
: The sitemap filename
|
- A sitemap.xml file in the root of the [`publishDir`] using the built-in [sitemapindex.xml] template
|
||||||
|
|
||||||
If provided, Hugo will use `/layouts/sitemap.xml` instead of the internal `sitemap.xml` template that ships with Hugo.
|
## Configuration
|
||||||
|
|
||||||
## Sitemap Templates
|
Set the default values for [change frequency] and [priority], and the name of the generated file, in your site configuration.
|
||||||
|
|
||||||
Hugo has built-on Sitemap templates, but you can provide your own if needed, in either `layouts/sitemap.xml` or `layouts/_default/sitemap.xml`.
|
|
||||||
|
|
||||||
For multilingual sites, we also create a Sitemap index. You can provide a custom layout for that in either `layouts/sitemapindex.xml` or `layouts/_default/sitemapindex.xml`.
|
|
||||||
|
|
||||||
## Hugo’s sitemap.xml
|
|
||||||
|
|
||||||
This template respects the version 0.9 of the [Sitemap Protocol](https://www.sitemaps.org/protocol.html).
|
|
||||||
|
|
||||||
```xml
|
|
||||||
{{ printf "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\" ?>" | safeHTML }}
|
|
||||||
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
|
|
||||||
xmlns:xhtml="http://www.w3.org/1999/xhtml">
|
|
||||||
{{ range .Data.Pages }}
|
|
||||||
<url>
|
|
||||||
<loc>{{ .Permalink }}</loc>{{ if not .Lastmod.IsZero }}
|
|
||||||
<lastmod>{{ safeHTML ( .Lastmod.Format "2006-01-02T15:04:05-07:00" ) }}</lastmod>{{ end }}{{ with .Sitemap.ChangeFreq }}
|
|
||||||
<changefreq>{{ . }}</changefreq>{{ end }}{{ if ge .Sitemap.Priority 0.0 }}
|
|
||||||
<priority>{{ .Sitemap.Priority }}</priority>{{ end }}{{ if .IsTranslated }}{{ range .Translations }}
|
|
||||||
<xhtml:link
|
|
||||||
rel="alternate"
|
|
||||||
hreflang="{{ .Lang }}"
|
|
||||||
href="{{ .Permalink }}"
|
|
||||||
/>{{ end }}
|
|
||||||
<xhtml:link
|
|
||||||
rel="alternate"
|
|
||||||
hreflang="{{ .Lang }}"
|
|
||||||
href="{{ .Permalink }}"
|
|
||||||
/>{{ end }}
|
|
||||||
</url>
|
|
||||||
{{ end }}
|
|
||||||
</urlset>
|
|
||||||
```
|
|
||||||
|
|
||||||
## Hugo's sitemapindex.xml
|
|
||||||
|
|
||||||
This is used to create a Sitemap index in multilingual mode:
|
|
||||||
|
|
||||||
```xml
|
|
||||||
{{ printf "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\" ?>" | safeHTML }}
|
|
||||||
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
|
|
||||||
{{ range . }}
|
|
||||||
<sitemap>
|
|
||||||
<loc>{{ .SitemapAbsURL }}</loc>
|
|
||||||
{{ if not .LastChange.IsZero }}
|
|
||||||
<lastmod>{{ .LastChange.Format "2006-01-02T15:04:05-07:00" | safeHTML }}</lastmod>
|
|
||||||
{{ end }}
|
|
||||||
</sitemap>
|
|
||||||
{{ end }}
|
|
||||||
</sitemapindex>
|
|
||||||
```
|
|
||||||
|
|
||||||
## Configure `sitemap.xml`
|
|
||||||
|
|
||||||
Defaults for `<changefreq>`, `<priority>` and `filename` values can be set in the site's config file, e.g.:
|
|
||||||
|
|
||||||
{{< code-toggle file="config" >}}
|
{{< code-toggle file="config" >}}
|
||||||
[sitemap]
|
[sitemap]
|
||||||
changefreq = "monthly"
|
changefreq = 'monthly'
|
||||||
|
filename = 'sitemap.xml'
|
||||||
priority = 0.5
|
priority = 0.5
|
||||||
filename = "sitemap.xml"
|
|
||||||
{{</ code-toggle >}}
|
{{</ code-toggle >}}
|
||||||
|
|
||||||
The same fields can be specified in an individual content file's front matter in order to override the value assigned to that piece of content at render time.
|
changefreq
|
||||||
|
: How frequently a page is likely to change. Valid values are `always`, `hourly`, `daily`, `weekly`, `monthly`, `yearly`, and `never`. Default is `""` (omitted from rendered sitemap).
|
||||||
|
|
||||||
|
filename
|
||||||
|
: The name of the generated file. Default is `sitemap.xml`.
|
||||||
|
|
||||||
|
priority
|
||||||
|
: The priority of a page relative to any other page on the site. Valid values range from 0.0 to 1.0. Default is `-1` (omitted from rendered sitemap).
|
||||||
|
|
||||||
[pagevars]: /variables/page/
|
## Override Default Values
|
||||||
|
|
||||||
|
Override the default values for a given page in front matter.
|
||||||
|
|
||||||
|
{{< code-toggle file="news.md" fm=true >}}
|
||||||
|
title = 'News'
|
||||||
|
[sitemap]
|
||||||
|
changefreq = 'weekly'
|
||||||
|
priority = 0.8
|
||||||
|
{{</ code-toggle >}}
|
||||||
|
|
||||||
|
## Override Built-in Templates
|
||||||
|
|
||||||
|
To override the built-in sitemap.xml template, create a new file in either of these locations:
|
||||||
|
|
||||||
|
- layouts/sitemap.xml
|
||||||
|
- layouts/_default/sitemap.xml
|
||||||
|
|
||||||
|
When ranging through the page collection, access the _change frequency_ and _priority_ with `.Sitemap.ChangeFreq` and `.Sitemap.Priority` respectively.
|
||||||
|
|
||||||
|
To override the built-in sitemapindex.xml template, create a new file in either of these locations:
|
||||||
|
|
||||||
|
- layouts/sitemapindex.xml
|
||||||
|
- layouts/_default/sitemapindex.xml
|
||||||
|
|
||||||
|
## Disable Sitemap Generation
|
||||||
|
|
||||||
|
You may disable sitemap generation in your site configuration:
|
||||||
|
|
||||||
|
{{< code-toggle file="config" >}}
|
||||||
|
disableKinds = ['sitemap']
|
||||||
|
{{</ code-toggle >}}
|
||||||
|
|
||||||
|
[`publishDir`]: {{< relref "getting-started/configuration#publishdir" >}}
|
||||||
|
[change frequency]: <https://www.sitemaps.org/protocol.html#changefreqdef>
|
||||||
|
[priority]: <https://www.sitemaps.org/protocol.html#priority>
|
||||||
|
[sitemap protocol]: <https://www.sitemaps.org/protocol.html>
|
||||||
|
[sitemap.xml]: <https://github.com/gohugoio/hugo/blob/master/tpl/tplimpl/embedded/templates/_default/sitemap.xml>
|
||||||
|
[sitemapindex.xml]: <https://github.com/gohugoio/hugo/blob/master/tpl/tplimpl/embedded/templates/_default/sitemapindex.xml>
|
||||||
Reference in new issue
Block a user