MODX Community

Hreflang tags with Lingua - simple method

Just a quick tip for anyone who wants to do this.

Following this: https://support.google.com/webmasters/answer/189077

Chunk code:

<link rel="alternate" hreflang="en"
    href="[[!++site_url]][[lingua.getHrefLang? &id=`[[*id]]` &field=`alias` &page_language=`en`]].html" />
<link rel="alternate" hreflang="fr"
    href="[[!++site_url]][[lingua.getHrefLang? &id=`[[*id]]` &field=`alias` &page_language=`fr`]].html" />
<link rel="alternate" hreflang="es"
    href="[[!++site_url]][[lingua.getHrefLang? &id=`[[*id]]` &field=`alias` &page_language=`es`]].html" />
<link rel="alternate" hreflang="ru"
    href="[[!++site_url]][[lingua.getHrefLang? &id=`[[*id]]` &field=`alias` &page_language=`ru`]].html" />
<link rel="alternate" hreflang="zh"
    href="[[!++site_url]][[lingua.getHrefLang? &id=`[[*id]]` &field=`alias` &page_language=`zh`]].html" />
<link rel="alternate" hreflang="x-default"
    href="[[!++site_url]][[lingua.getHrefLang? &id=`[[*id]]` &field=`alias` &page_language=`en`]].html" />

Create a new Snippet called ‘lingua.getHrefLang’ which is a copy of the ‘lingua.getValue’ Snippet but with one change. At line 43, change:

'lang_code' => $modx->cultureKey
to
'lang_code' => $page_language

This will only work if you assign translated aliases for each resource in each language. I use the alias in my menus ie: <a href="[[lingua.getValue? &id=3 &field=alias]].html"

2 Likes