How to: Create a Multi-Language Blog

Using WordPress plugins to automatically translate blog content

This is the easiest solution. A few WordPress plugins allows you to put some clickable flags in your sidebar, that will automatically translate your content to another language using, for example, Google Translate.
This is a great solution if you’re looking for a way to make your content available for people that doesn’t speak your language, but you guessed it, the automatic translation isn’t as good as a human translated text.

Anyways, if you’re interested in theses plugins, here’s a small list. I didn’t tried them all myself, though.

Using categories and a few hacks

If you’re a perfectionist, you probably don’t really like the automatic translations provided by Google. In my opinion, if it’s okay for a personal blog, but automatic translations should be avoided on a professional blog.

For this hack, we will use categories. Go to your dashboard and create categories/sub-categories following the example below:

English
    One category
    Two categories
Français
    Une catégorie
    Deux catégories
Deutsch
    Eine kategorie
    Zwei kategorien

Of course this is just an example, you can add as many categories as you want. The thing to remember here is that we’re using top level categories for languages, each of them having sub-categories organized by topics.

Now, you should post at least one example post in each sub-category. Once you’re done, we can start editing our header.php template.

The idea is quite simple: First, we must display the available languages, and then, a menu with the sub-category related to the current language. We must also think about defining a default language, which will be English in this example.

1 – Creating the language menu
Insert the following code where you want the language selection menu to appear:

<?php
wp_list_categories('title_li=');
$lang = 1; // Sets the default language
?>

As you can see, we just need the good old wp_list_categories() function to get our available languages.
Then, we have to keep a php variable containing the id of the top level category, in order to display the navigation menu in the selected language. We must give a default value to the $lang variable. This default value will be the category id of the language you want to set as default.

I noticed that many people should have trouble to find the ID of a particular category. If you do, you should definitely read this post.

2 – Navigation menu
Now, we got a php variable containing the id of the mother category. As the top-level categories are languages, we know which language the user is using.

We simply have to display second-level categories.

<ul>
<?php
wp_list_categories('title_li=&child_of=$lang');
?>
</ul>

3 – Retrieving category
Here’s a sample code you can use to automatically list posts from a category. This code get the current category (in this example, the language) and then displays the related posts.

<?php foreach((get_the_category()) as $category) {
      	$thecat = $category->cat_ID . ' ';
    	query_posts('child_of='.$thecat);
	if (have_posts()) : while (have_posts()) : the_post();
	   //Classic WP loop
	endwhile;endif;
?>
  • Leave a Comment
  • How To: Insert Ads Only After the First Post

    Have you ever tried to insert advertisements (or any bit of code really) into the WordPress post loop, then found that it will insert the banner after each post?   It really depends on what you are going for, but this usually will not be an ideal solution to placing advertisements between posts on your homepage.   If you’d like to place something only after the first post in the WordPress post loop, here is a quick hack you can do to tell WordPress to only display it after the first post.

    Simply go to your themes homepage and look for the following code:

    <?php endwhile; ?>

    Immediately before this code, place the following code:

    <?php if(!$show_ads){ ?>
    Insert Code Here
    <?php $show_ads = 1; } ?>

    Obviously you’ll want to replace Insert Code Here with your code.  Told you it was easy!

  • Leave a Comment
  • How To: Display Adsense On Your First Post Within The Loop

    In this guide you’ll learn how to display Adsense on just your first post within the Loop. Sure, there may be plugins that will do this for you. This guide, however, will use code examples to accomplish the same thing by editing your WordPress theme.

    The first step is to open up your index.php file in your theme editor. Find the following line:

    <?php if(have_posts()) : ?>

    Just above that, insert the following like this:

    <?php $i = 1; ?>
    <?php if(have_posts()) : ?>

    Now, scroll down a bit until you find this line:

    <?php endwhile; ?>

    Insert the following above it, like so:

    <?php $i++; ?>
    <?php endwhile; ?>

    The final step is to insert your AdSense code. Locate where exactly you’d like it within the Loop, and place it between a conditional tag like this:

    <?php if ($i == 1) { ?> [YOUR ADSENSE CODE HERE] <?php } ?>

    And that’s all you have to do. You can be creative with this code as well. You could add a certain css style class to the top post in your Loop. It doesn’t even have to be the first either, as you can just change the number in $i == 1 to whatever you want. This same method can be used in other archive templates such as archive.php.

    You can display ads with WordPress in other ways as well, including plugins such as WhyDoWork Adsense Plugin (formerly Shylock Adsense).

  • Leave a Comment
  • Ultimate Guide to the WordPress Loop

    One of my favorite parts about using WordPress for my blogs is getting to work with PHP code, which I find to be much easier to write/hack.   For those that are shy around code, it really isn’t that difficult to get ahold of the basics of PHP, so WordPress is the right place for you. 

    One great area to start is learning how the WordPress loop works.  This is a basic function of blogging used to display the most recent X number of posts on your blog’s homepage (for traditional blogs).   Rather than go into to much detail here, I’d like to point you towards a new post by Themelab which is designed to be the Utlimate Guide to the WordPress loop

    This post definitely lives up to its name and goes beyond just showing you how to do something.  It actually explains how and why it works, and includes screenshots with many of the examples.   If you have any interest in learning about the WordPress loop you may want to read through this post and/or bookmark it for future reference. 

  • Leave a Comment