Add code into on a per page/post basis

As you said per page/post basis, this would work for each post

add_action('wp_head', 'add_link_in_head');
function add_link_in_head()
{
    global $post;
    if(!empty($post))
    {
        $alternate = get_post_meta($post->ID, 'alternate', true);
        $hreflang = get_post_meta($post->ID, 'hreflang', true);
        if(!empty($alternate) && !empty($hreflang))
        {
            ?>
                <link rel="alternate" href="https://wordpress.stackexchange.com/questions/110260/<?php echo $alternate; ?>" hreflang="<?php echo $hreflang; ?>" />
            <?php
        }
    }
}

If there is no $alternate and $hreflang then there would be no link.

Leave a Comment