0

I can't work out how to output <link rel="alternate"> for each language variation?

Is there any easy way to do this please?

Many thanks!

  • 1
    Is this with Craft 3's translation features? – Jay Aug 17 '18 at 10:10
  • 1
    Craft 2 or Craft 3? – Brad Bell Aug 17 '18 at 18:07
  • If you use SEOmatic, this is something it does for you automatically, FWIW. – andrew.welch Sep 23 '18 at 23:33
0

If you're looking to get the locale code for each site's language globally, it'd be something like this:

{% for locale in craft.i18n.getSiteLocales() %}
    {{ locale }}
{% endfor %}

If you're in the context of an entry, and you want to get the full URL and locale code for each site that entry is enabled it, it'd be similar to the answer here: https://craftcms.stackexchange.com/a/23751/57

{# Grab the entry #}
{% set entry = craft.entries.id(2).one() %}

{# loop through all of the supported sites for the entry #}
{% for siteInGroup in entry.getSupportedSites() %}
    {% set site = craft.app.sites.getSiteById(siteInGroup['siteId']) %}

    {# Grab the entry for the given site #}
    {% set entryForSite = craft.entries.id(entry.id).siteId(site.id).one() %}

    {# output the site-specific entry info #}
    <link rel="alternate" href="{{ entryForSite.url }}" hreflang="{{ site.language }}">{{ entryForSite.title }}</link>
{% endfor %}
| improve this answer | |
  • Hi Brad - many thanks for your reply. But unfortunately, that only generates the 'homepage' url for each site. The link rel="alternate" mark-up needs to output the alternative URL for each individual page of the site. I hope that makes sense? – MGX Aug 28 '18 at 8:58
  • Sorry, I don't think I'm following... can you update the original question with some specific examples of what you're looking for? – Brad Bell Aug 28 '18 at 23:30
  • Hi Brad - many thanks for your reply. This Google Search Console give the best explanation of what's required: support.google.com/webmasters/answer/189077?hl=en So with regard to a Craft site, for each individual page of the site we need to be able to output the link rel="alternate" mark-up that shows the full URL for each language version, together with the respective hreflang value. This might be something like: – MGX Aug 29 '18 at 7:43
  • Hi Brad - many thanks for your reply. This Google Search Console give the best explanation of what's required: support.google.com/webmasters/answer/189077?hl=en So with regard to a Craft site, for each individual page of the site we need to be able to output the link rel="alternate" mark-up that shows the full URL for each language version, together with the respective 'hreflang' value. So each page has a unique set of mark-up for this in the head. But...the code you posted earlier just outputs the homepage of each language variation. I hope that makes sense? Thanks! – MGX Aug 29 '18 at 7:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.