WordPress: single posts using different language

A technique to include single Wordpress posts in a different language than the main language of the blog.

Publishing a multilingual blog is a hard task; you have to translate and maintain every single post, besides translating all the interface. But what about if you have a simple monolingual blog, and occasionally want to include a post using a different language? You can just write the title and the content in that language, but, as W3C guidelines says, additionally you should…

Clearly identify changes in the natural language of a document’s text and any text equivalents (e.g., captions). [Priority 1]

So here you are an easy way to mark a single post using a different language, using custom fields in WordPress:

1. Modify your template file(s) (tipically single.php, index.php, archive.php) where the following piece of code appears (the div block containing every single post):

<div class="post" id="post-<?php the_ID(); ?>">

Modify them by adding the highlighted code (to check if there’s a custom field called lang for that single post and include it as the language of the post, if not empty):

<div class="post" id="post-<?php the_ID(); ?>"<?php $custom = get_post_custom_values('lang'); if ( $custom != '' ) echo ' lang="'. $custom[0] . '"'; ?>>

2. For every single post written in a different language than the blog main language, add a custom field called lang and assign it the post’s language code (‘en’ for English; ‘es’ for Spanish, …)

Adding custom field 'lang' with value 'en'

3. And that’s all. Now, every post may be identified with its own language. Of course, you don’t need to include that field if the language of the article is the same language than the blog, as long as the main language is identified in the header of the web page.

Please, let me know it this works for you or if you find any bugs or improvements.

Leave a Reply

Your email address will not be published. Required fields are marked *