• Resolved Xtrace

    (@xtrace)


    Here’s just a little snippet that you can use to add the language urls to you page head, which is for search engine’s to serve hits for a user in his own language. You can add the code to your theme’s functions.php file.

    add_action('wp_head','wp_head_alternate_lang', 1);
    
    function wp_head_alternate_lang() {
    
    	$output = array();
    
    	if (function_exists('the_bilingual_link')) {
    
    		$args = array(
    			"echo" => false,
    			"url_only" => true
    		);
    		$blg = get_option( 'BilingualLinkerGeneral' );
    
    		if ($blg['numberoflanguages'] > 0) {
    
    			for ($i=1;$i<=$blg['numberoflanguages'];$i++) {
    
    				$langArgs = $args;
    				$langArgs['language_id'] = $i;
    				$altLinkResult = the_bilingual_link($langArgs);
    
    				if ($altLinkResult !== '' && $altLinkResult !== 'https://' && $altLinkResult !== 'https://') {
    
    					$output[] = '<link rel="alternate" hreflang="' . $blg["language" . $i . "langcode"] . '" href="' . $altLinkResult . '" />';
    				}
    			}
    		}
    	}
    
    	if (!empty($output)) {
    
    		echo "<!-- alternate languages -->\n" . implode("\n", $output);
    	}
    }

    Example output in the head:

    <!DOCTYPE html>
    <html lang=”en”>
    <head>
    <title>website title</title>
    <!– alternate languages –>
    <link rel=”alternate” hreflang=”de” href=”https://www.yoursite.de&#8221; />
    <link rel=”alternate” hreflang=”es” href=”https://es.yoursite.com&#8221; />
    </head>

    where the href’s are entered in the box on your page/post

Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘Snippet: alternate hreflang for head’ is closed to new replies.