I want to add this code only for one post before </head>

<link rel="alternate" hreflang="en-gb" href="http://abc.com"/>

What code shall i use?

2 Answers 2


Write a function, and define it as a callback on the wp_head hook:

 * print a alternate link on head for a several post
 * @wp-hook wp_head
 * @return void
function wpse_162849_print_alternate_link() {

    //don't do anything expect on singular pages
    if ( ! is_singular() )

    // check the global post
    if ( empty( $GLOBALS[ 'post' ] )

    // compare the post-ID
    $your_post_ID = 42;
    if ( $your_post_ID != $GLOBALS[ 'post' ]->ID )

    echo '<link rel="alternate" hreflang="en-gb" href="http://abc.com"/>';
add_action( 'wp_head', 'wpse_162849_print_alternate_link' );

Change the post ID to your needs. You can also implement another logic to choose the right post, for example by a special post meta.


WordPress has Conditional Tags which can be used to determine various things, e.g. if we are on the right post. The wp_head action hook can be used to add things to the head section. For below exemplary post is use the conditional tag is_single(), which can be used for any post type, but attachment and page, it takes one parameter, which is $post and can be the ID, title or slug.

add_action( 'wp_head', 'wpse162894_add_alternate_lang_href_to_post' );
function wpse162894_add_alternate_lang_href_to_post() {
    if ( is_single( 'your-posts-slug' ) ) {
        $link_rel = '<link rel="%s" hreflang="%s" href="%s" />';
        $rel      = 'alternate';
        $hreflang = 'en-gb';
        $href     = 'http://an-url.org';
        printf( $link_rel, $rel, $hreflang, $href );

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.