English > General Questions
Alternate Meta Tags
jstead:
Thanks Harald,
Worked a trick!
I have tried to get tricky and have it display the 2 digit code for any unified languages, but display the 4 digit code where I have 2 variations (en-US/en-GB and fr-FR/fr-CA)
The below code changed every link dependant on if FR-CA/en-US was selected, not just the loop entry for a single line (FR/US showed 4 digit codes for ALL <link> elements while the other languages showed 2 digit)
--- Code: ---} else {
$tag = $current->getTag();
$lang->active = false;
if ($tag == 'fr-CA' || $tag == 'en-US') {
if (($lang->menutitle != '') && ($params->get('alternatetag'))) {
$doc->addHeadLink($lang->link, 'alternate', 'rel', array(
'type' => 'text/html',
'hreflang' => $lang->lang_code,
'lang' => $lang->lang_code,
'title' => $lang->menutitle
));
}
} else {
if (($lang->menutitle != '') && ($params->get('alternatetag'))) {
$doc->addHeadLink($lang->link, 'alternate', 'rel', array(
'type' => 'text/html',
'hreflang' => substr($lang->lang_code, 0, 2),
'lang' => $lang->lang_code,
'title' => $lang->menutitle
));
}
}
}
--- End code ---
Where could I move the IF statement to have it apply to the loop through each language WITHIN a single page.
(so only US FR are 4 digit)
--- Code: --- <link href="/us/" rel="alternate" type="text/html" hreflang="en-US" lang="en-US" title="Home" />
<link href="/de/" rel="alternate" type="text/html" hreflang="de" lang="de-DE" title="Startseite" />
<link href="/fr/" rel="alternate" type="text/html" hreflang="fr" lang="fr-FR" title="Accueil" />
<link href="/ca/" rel="alternate" type="text/html" hreflang="fr-FR" lang="fr-CA" title="Accueil" />
<link href="/it/" rel="alternate" type="text/html" hreflang="it" lang="it-IT" title="Pagina iniziale" />
<link href="/es/" rel="alternate" type="text/html" hreflang="es" lang="es-ES" title="Inicio" />
<link href="/cn/" rel="alternate" type="text/html" hreflang="zh" lang="zh-CN" title="起始界面" />
--- End code ---
Thanks,
Jason
Harald Leithner:
I still don't think that it makes any difference, but for this to work the concept have to be changed a little bit.
You have to make 2 loops, to check if there is any language 2 times like fr and en in you case. Then you have to set the correct hreflang and add it to the header.
Anyway I think its a calculation over head for each site expression and google will not need this.
And here the answer for your question ;-)
--- Code: ---if (substr($lang->lang_code, 0, 2) == 'en' || substr($lang->lang_code, 0, 2) == 'fr' ) {
$doc->addHeadLink($lang->link, 'alternate', 'rel', array(
'type' => 'text/html',
'hreflang' => $lang->lang_code,
'lang' => $lang->lang_code,
'title' => $lang->menutitle
));
} else {
$doc->addHeadLink($lang->link, 'alternate', 'rel', array(
'type' => 'text/html',
'hreflang' => substr($lang->lang_code, 0, 2),
'lang' => $lang->lang_code,
'title' => $lang->menutitle
));
}
--- End code ---
jstead:
Thanks Harald,
Worked perfectly as usual!
From what I read, using the 4 digit code will indicate for Google to target the search results to the specific Country, whereas using the 2 digit code will target anyone who's system is setup as the language. So for the amount of countries we target, this addition awesome! Thank you again.
Just noticed, it also mentions you should include the tag for the current language you are on, as well as the other language versions. (is this what is removed in the first part of the If/else statement?)
Thanks,
Jason
Reference
https://support.google.com/webmasters/answer/189077?hl=en
Harald Leithner:
You are right, I should fix this. Attached is a new version with a better handling for this case. (and a database function fix).
If you could deploy this version and give me feed back if everything works fine. Then I will release it to public.
jstead:
Hi Harald,
Thank you for this, will test ASAP.
We have just launched our website live, so I will need to wait until the development environment has been re-established.
Hopefully this should be ready tomorrow, will update once I have tested.
Kind Regards,
Jason
Navigation
[0] Message Index
[#] Next page
[*] Previous page
Go to full version