Is there plugin (or an easyish way) to add code to the <head>
section on a per page/post basis?
I have a multilingual website and I need to implement the rel=”alternate” hreflang markup, so need a way to define rel="alternate"
individually on each page/post.
I have searched the WordPress plugins but haven’t found any thing thus far. I’m not really a coder so can’t create something myself, unless it was rather easy!
UPDATE
OK, I’m having a go myself trying to create a custom field for this functionality, this is what I have so far:
<link rel="alternate" href="<?php
while (have_posts()) : the_post();
$alternate = get_post_meta($post->ID, 'alternate', false);
if ($alternate) {
echo $alternate[0];
}
endwhile;
?>" hreflang="<?php
while (have_posts()) : the_post();
$hreflang = get_post_meta($post->ID, 'hreflang', false);
if ($hreflang) {
echo $hreflang[0];
}
endwhile;
?>" />
Now this works fine if I only want to add one alternate page. When I try to add any more, it will only add one instance of rel="alternate"
, with the last added replacing the original.
Also, on any pages that I haven’t defined any of these custom fields, I’m getting an empty tag:
<link rel="alternate" href="" hreflang="" />
Can someone please point me in the right direction to correct these issues?
As you said per page/post basis, this would work for each post
If there is no $alternate and $hreflang then there would be no link.
Use the below code in the
<head>
section:For more information you can visit here.
I don’t know how you are saving your data but assuming that you are using the built in “Custom Fields” meta box your multiple alternate pages should come through as an array.
However, I see a potential issue. If you are to have multiple associated
alternate
/hreflang
pairs per post, then you need to save them as pairs in the database. Unless you do, there will be no way to tell whichhreflang
goes with whichalternate
. You will need to construct your own meta box for that.If you are using Transposh (The plugin that I have developed) For managing your multilingual sites, the rel=alternate will be added automatically (with the option to disable it)
However, the reason I am answering this, is that in my experience, the results of adding this tag have actually been abysmal, so I am not sure that adding it is helpful at all.
Good luck
sorry for this i have not 50 reputation to add a comment
dot1‘s answer is perfect for include link alternate in head section get value from post meta but now you need to add custom meta keys for hreflang and alternate values for each page/post from post edit screen or create a meta box for to do this.
hope this will help you, you may change function names etc.