Using WordPress for your website is a great way to create a well-designed website. It is also very user-friendly and allows for a lot of customization. But one missing features is to assign a language to a specific post. This is especially useful if you are creating a multi-lingual website, or if you want to target a specific country : language is also a niche ! Write content in other language with Writebot
In this guide, we’ll show you how to change the language for a specific post in WordPress without using a plugin. We’ll also cover how to change the language for the entire website.
Why Change the Language for a Specific Post?
If you are creating a multi-lingual website or want to target multiple markets, then it is important to assign a language to each post. This will ensure that visitors to your website can easily switch languages and read your content in the language of their choice. It also makes it easier for search engines to index your content in the right language.
You might also want to assign a language to a specific post if you are writing a post in a language other than your website’s default language. This will ensure that the post is displayed correctly on your website.
How to Change the Language for a Specific Post in WordPress
Step 1: Enable Custom Fields
Firstly, you need to enable the custom fields for Posts, follow the 2 next steps. Start a new post or open an existing one to open the Options Panels (see below)
Now you can access to the Preferences and select Panels to activate the options Custom Fields
Once activated, you’ll see new fields appearing below your Posts :
Now you can Add Custom Field with the name Schema and Value corresponding to the actual schema you want to use.
For instance:
- For french from France add the following : Name: language – Value : lang=”fr-FR”
- For english US add the following : Name: language – Value : lang=”en-US”
- For english GB add the following : Name: language – Value : lang=”en-GB”
Check the complete list of ISO HTML Language
Step 2 : Map the Custom field inside header.php
Now this step is more technical, but easy to understand. To display a post, WordPress is populating a template page with properties coming from your Database (for instance the content of your post, the tags). In order to add a specific language to your posts, you need to tell WordPress to map the Custom Fields inside the header of your blog post :
In Appearance, go to Theme File Editor
Now select the header.php inside the Theme Files :
Add the following PHP code inside the header, instead of the <html <?php language_attributes();?>> tag
Before :
<html <?php language_attributes();?>>
After:
<html
<?php
$language = get_post_meta(get_the_ID(), 'language', true);
if(!empty($language)) {
echo $language;
}else{
language_attributes();
}
?>>
What does mean this code :
- If among all Custom Fields, there is one named ‘language‘
- Then write the Value of the Custom Field inside the header
- Else set the language to the current WordPress value (coming from language_attributes() )
Now, you could specify for each posts the language. This method also work to add other metadata inside specific posts.
Conclusion
Changing the language for a specific post in WordPress is easy and can be done without using a plugin. All you need to do is two little step. You can also change the language for your entire website by going to your WordPress dashboard and selecting “Settings” > “General”. The reason to do that is to extend your market by targeting specific countries ! If you don’t master a specific language, use our tools to create content in +30 languages