Add language attribute per post in WordPress

Wordpress codingThere are numerous ways to create a multilingual WordPress website. However, there is not much information to be found if you just want to have WordPress website in one language, while only writing specific blog posts in another language. For example: your mother tongue is Dutch. To gain an international audience for your website, the main language of your website and blogposts is English. However, from time to time, you want to write an article in Dutch. To indicate this, according to HTML5 standards, you should add something like this:

<article lang="nl">Nederlandstalige tekst komt hier te staan</p>

To add the lang=”html” attribute to specific WordPress blog posts, I had to come up with a little trick that allowed me to only add this attribute to specific posts. So first, I added a tag to each of my Dutch posts and named them nl.

Next, I edited my content.php file and replace this:

<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>

with this:

<article id="post-<?php the_ID(); ?>" <?php post_class(); ?> <?php if ( has_tag( 'nl' ) ) { echo 'lang="nl"';}?>>

This simple if statement will check if that specific post has the attribute ‘nl’ and if so, it will add lang=”nl” to it. This indicates that while my website is in English, that specific part of the website is written in Dutch.

This is great for SEO, too!

I also created a custom page for that tag. Since some tags now have the nl tag, I wanted to give an explanation on that page when people click on the tag. So I created tag-nl.php within my theme directory, copied the contents of the existing tag.php and pasted it within tag-nl.php. Then, I added a bit of information for the visitors who viewed that page:

<header class="archive-header"><h1 class="archive-title">Tag Archives: nl</h1><p lang="en">This is an overview of all the blogpost written in Dutch.</p><p lang="nl">Dit is een overzicht van alle Nederlandstalige blog berichten.</p> </header>



Author Bio

Thank you for your interest in my blog! On this miniblog, I write mostly short (technical) blog posts that might interest other people. Read more about me or feel free to contact me.


9 thoughts on “Add language attribute per post in WordPress

  1. Brilliant, thanks for this.

    I’m no php genius and have a similar issue to you; a site which is about 40% English and 60% Italian (no need for translator plugins because each page is for a specific language group (teachers / students of English).

    Looking at page source, my wp pages and posts all have English language attributes and to save time, it would be best if I used your solution to flag the pages that are in English and have Italian as the default display language. How do I change that? I can’t see where.

    Many thanks, in any event.

  2. Hmm…I just went to my site to test this out and discovered I don’t have a content.php file. I assume the theme uses other templates but I’m damned if I know which I would add your code to.

    1. Hello Brian
      you could also include the language tag in the header.php file, which can be found in /wp-content/themes/themename.
      It’s possible to include it within the html-tag, such as < html lang="< ?php if ( has_tag( 'it' ) ) { echo 'lang="it"';}?>">. This could output < html lang="it"> if the post has the it tag (for Italian posts).

  3. Pingback: Simple Language-Defined posts in WordPress | Jugglingsource
  4. Hi,

    very good article! I have a similar problem and am looking for a solution. I’d like to post the majority of my posts in my language and from time to time to publish a post in English but would not like the posts written in English to be displayed in the timeline mixed with the posts written in my language. And, of course, I would like to avoid multilingual setup.

    Do you know any convenient way to exclude the posts written in English from showing in the timeline? The exclusion could be based on tag but the solution shouldn’t break the functionality of Infinite Scroll and AJAX-load-more-like plugins.

  5. Hi Thomas, I like this hack because of the beauty of its simplicity. I’ve been looking for a proper solution and found none so far. Using polylang for some time now, I can’t list posts in different languages on one page. Do you still use it? Or are there other gentle solutions after so many years?

    1. Hi Wiebe
      Given I changed themes in WordPress a couple of times since posting this, I haven’t always implemented this ‘hack’.
      However, I find that search engines are quite good at making the difference between multiple languages on 1 domain.

      Another trick could be to use categories and use it in the URL.
      Example: category ‘NL’ can be used as
      In this case, search engines may automatically recognise the language difference. And/or I’m sure with Bing Webmaster Tools you can assign geolocations to their fitting URL scheme.

      Good luck!

  6. Hi just wanted to know if i can add langugae tag to y one language wordpress site ? Is there a trick to do this so booble and bing show that i have language tag on my pages and blog posts ? tx infront

Leave a Reply to Thomas Cancel reply

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