Language Switcher loads CSS files from an old URL

If the CSS files of the language switcher are loading from an old URL after moving the website follow the steps below:

  • Go to “WordPress Dashboard > Support > Troubleshooting”.
  • Click the “Clear the cache in WPML” button.

The same thing is available via wp-cli command below:

wp wpml clear-cache

The command above is available in WPML 4.4.11 and above.

Display post translations links on blog, archive page

“Links to the translation of posts” only works for a single post. Some clients asking to add links to translations on blog or archive page for each post.

I encountered two clients so far so I am sharing the script that I am using.

Please add this code in functinos.php file


function wpml_post_languages_in_loop() {
    $post_trid = apply_filters('wpml_element_trid', NULL, get_the_ID(), 'post_' . get_post_type());
    
    if (empty($post_trid)){
        return;
    }
    
    $active_langs = apply_filters( 'wpml_active_languages', NULL, 'skip_missing=0' );
    $translations = apply_filters('wpml_get_element_translations', NULL, $post_trid, 'post_' . get_post_type());
    if (is_array($translations) && !empty($translations)) {
        
        $langs = array();
        foreach ($translations as $translation) {
            if (is_object($translation) && isset($active_langs[$translation->language_code]) && $translation->element_id != get_the_ID()) {
                $langs[] = 'url-to-image';
            }
        }
        
        if (!empty($langs)) {
            echo __('This post is also available in', 'wpml') . ': ' . implode('  ', $langs);
        }
    }
}

Then use this function in the template file to display posts links.


wpml_post_languages_in_loop();

Inline Language Switcher – Flags/Code/Code+Flags

Inline (horizontal) custom language selector with different options such as Flags only, Code only, Flags + Code with divisor.
Take the time to study the code.

To use call wpml_custom_language_selector(); in a template

function wpml_custom_language_selector(){
	// check if wpml is installed
	if (function_exists('icl_get_languages')) {
		// get active languages. Available params here: 
		// http://wpml.org/documentation/getting-started-guide/language-setup/custom-language-switcher/
		$languages = icl_get_languages('skip_missing=0&orderby=code&order=desc');			
		if(!empty($languages)){
			echo '<div class="lang_selector">';
				foreach($languages as $l){
				// from this point on, uncomment whatever output you like
					// flags only - do not display current language 
					/* if(!$l['active']){
						echo '<a href="'.$l['url'].'">
							<img src="'.$l['country_flag_url'].'" alt="'.$l['language_code'].'" />
						</a>';
					} */
					
				// the output code options below will display the current language
					// this adds an "active" class to the current language anchor
					$class = $l['active'] ? ' class="active"' : NULL;
					// code only
					/*$langs .=  '<a ' . $class . ' href="'.$l['url'].'">' . strtoupper ($l['language_code']). '</a> | ';*/
					//flag-code divided by a |
					/*$langs .=  '<a ' . $class . ' href="' . $l['url'] . '"><img src="' . $l['country_flag_url'] . '" alt="' . $l['language_code'] . '" />' . strtoupper ($l['language_code']). '</a> | ';*/
				}
				// strip the empty space and | from last language
				$langs = substr($langs,0,-3);
				echo $langs;
			echo '</div>';
		}
	}
}