How to change the language for specific category posts in WordPress

Using such plugins as Polylang, qTranslate X or WPML will make your WordPress blog a multilingual site. However, these plugins are not lightweight. If you use a multilingual plugin, your site will be slowed.

Most posts in my blog are in Korean. I decided to create a new category which includes English posts only. I also wanted to change the language setting for the posts in the newly created category (such as "English"). Since the language of my WordPress is set to Korean, the language settings for all posts will be Korean too. Please refer to this article titled "WHO CARES WHAT LANGUAGE YOU USE?" for reasons to use proper language settings.

For this purpose, you can refer to this post which explains how to set the language on individual pages in WordPress. It's about changing the language setting on individual pages. Now, I want to change the language for all posts in a specific category. Therefore, I just need to change the conditional code which checks if individual pages are loaded to reflect all posts within a specific category.

I replaced the part "if (is_page()) { ... }" with the following:

if (is_single()) {
if (in_category('123')) {
$postLanguage = "en";
}
...
}

There are two instances. This will change the language for the posts in the specified category. Of course, you can use an array to include several categories (i.e. in_category(array('1', '2', '3')).)

Note: The code referenced above might cause problems. Instead, please refer to the following post:


1 개 댓글

Leave a Comment