When you need Free plugin that would make your WordPress site multilingual, qTranslate is a way to go. It works great, but I encountered a problem recently when I wanted to have multiple languages in attributes of Featured Image previously known as Post Thumbnails. I tried to insert qTranslate language tags in Title, Description, Caption, and most importantly Alternative Text but unfortunately nothing got translated when
the_post_thumbnail() was called.
In this article you will learn how to make those fields multilingual.
First we will examine how to store multilingual text when no tabs are available to switch between languages. Then we will explore where those attributes of Featured Image are stored and finally we will use PHP code to extract that information and use it when calling
Add multilingual text into the attributes of a featured image
Currently (qTranslate v2.5.34) there are no tabs available when filling fields of attributes for Featured Image, so we will have to insert qTranslate language tags ourself. For those unfamiliar with these tags, they look like this1<!--:en-->english text<!--:--><!--:de-->german text<!--:-->
For some reason Alt Text of Featured Image cannot save language tags
<!–:xx–>… <!–:–>so for that attribute we must use Quicktags instead. They are used like this:1[:en]english text[:de]german text
Location of Featured Image attributes in Database
the_post_thumbnail()doesn’t do anything in regard to translating our text, we will need to extract attributes of Featured Image ourselves. But to do that we must first know where the data is stored.
Featured Image thumbnails are stored in post_type table, same as regular posts but with post type of attachment. Inside those posts we will find data for most of the attributes of the featured image. Some other information is stored in the wp_postmeta table.
Where each attribute is stored is shown below:
DB Table: post_type
Column in post_type Featured Image attribute stored post_content Description post_title Title post_excerpt Caption
DB Table: wp_postmeta
meta_key column meta_value _thumbnail_id ID of that featured image in the post_type table _wp_attachment_image_alt Alt text
PHP code to add
In our example, we are going to use Alt Text and Title attributes for our Featured Image, but you could also include description and caption in the same way. Size of our thumbnail is set to 150×150. Adjust that size to your needs.
the_post_thumbnail()line with the following code:12345$thumbnail_id = get_post_thumbnail_id($post->ID);$thumbnail_image = get_post($thumbnail_id);$title=__($thumbnail_image->post_title);$alt_text = __(get_post_meta($thumbnail_id , '_wp_attachment_image_alt', true));the_post_thumbnail( array(150, 150),array( 'alt' => $alt_text,'title'=>$title));
Line by line explanation of the above code:
I hope you found this article useful. Please drop a comment or consider sharing it.