How to make your slider have multilingual support using qtranslate

If you have a multilingual site with slider which uses the featured images for slider images and you want to make it support different slide images depending on the language you might want to follow these simple steps (or you can hack it your way).

Problem one: WordPress doesn’t support multiple featured images.

Solution

  1. Get and install this plugin
  2. Add the following code to your theme (functions.php would be okay)
if (class_exists('MultiPostThumbnails')) {
  $type = 'slides_options';

  $thumb = new MultiPostThumbnails(array(
    'label' => 'Secondary Image',
    'id' => 'secondary-image',
    'post_type' => $type
    )
  );
}
add_image_size('nivo-secondary-image-thumbnail', 960, $nivo_h, true );

Here slides_options is the Custom Post Type which we use for the slider. If you’re not using custom post type for this you can simply change it to ‘post’.

Congrats! Your theme now supports multiple featured images. Hooray!

Now we have to make WordPress display the relevant image for the specific language.

For this purpose we use a function which determines if the default language of qtranslate is used or not and depending on this shows the featured or the secondary image.

function qtranslate_slide() {

 global $post, $q_config;

 $image_id = get_post_thumbnail_id();
 $language = qtrans_getLanguage();
 $upload_dir = wp_upload_dir();
 $image_data = wp_get_attachment_metadata($image_id);
 if ($q_config['default_language'] != $language) {
 if (class_exists('MultiPostThumbnails') && MultiPostThumbnails::has_post_thumbnail('slides_options', 'secondary-image')) :
 MultiPostThumbnails::the_post_thumbnail('slides_options', 'secondary-image', NULL, 'nivo-secondary-image-thumbnail');
 endif;
 }
 else {
   if (file_exists($upload_dir['basedir'].'/'.$image_data['file'])) {
   $image_url = wp_get_attachment_image_src($image_id,'full', true);
   return $image_url;
  }
 }
}

Now we have to change in our code where we display the images for the slider

$thumbnail = wp_get_attachment_image_src(some, arguments, here);

with our function

$thumbnail = qtranslate_slide();

And from now on we can use $thumbnail[0] as url for the relevant language, depending on the chosen language

Simple as this! Good luck!

P.S.: feel free to ask me something about that on twitter – 

!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=”//platform.twitter.com/widgets.js”;fjs.parentNode.insertBefore(js,fjs);}}(document,”script”,”twitter-wjs”);