Skip to content
  1. Extras
  2. Localizator3
  3. hreflang attribute

hreflang attribute ​

The getLocalizedCanonical snippet outputs link rel="canonical" for the current language and link rel="alternate" hreflang="..." for active languages. You do not need a custom markup snippet.

It adds hreflang="x-default" when a default-language URL is found. localizator3_default_language is compared to the language key, not cultureKey. If the setting is empty, the current language URL is used.

Parameters ​

NameDefaultDescription
idcurrent resourceResource ID. Empty null and '' use the current resource. Empty string if there is no resource
tplemptyChunk name or @name via pdoFetch. Placeholders: links (items with url, hreflang, is_current, language_key) and ready output. If the chunk is not found, the default markup is kept

Behavior ​

The snippet returns an empty string when:

  • there is no current localizator3_key;
  • no URL was built.

canonical and, with an empty setting, x-default appear only if the current key belongs to an active language with a non-empty URL.

Languages are sorted by rank, then by id. In markup, canonical belongs to the current language; x-default is output last.

The hreflang value comes from the language cultureKey, otherwise from the key. URLs are built by makeLocalizedUrl.

Call in head ​

fenom
{'!getLocalizedCanonical' | snippet : ['id' => $_modx->resource.id]}
modx
[[!getLocalizedCanonical? &id=`[[*id]]`]]

Sample output (order depends on rank):

html
<link rel="canonical" href="https://site.example/page.html">
<link rel="alternate" hreflang="ru" href="https://site.example/page.html">
<link rel="alternate" hreflang="en" href="https://en.site.example/page.html">
<link rel="alternate" hreflang="x-default" href="https://site.example/page.html">