Search code examples
phpwordpressmultilingual

Bilingual Wordpress Website


I am developing a wordpress website and I would like to have two instances of the website, one in english and the other in german. My goal is to when a user visits my website, the user is redirected to the correct language, based on his/her browser language. But I want to do this with only one instalation of wordpress. I can make duplicates of the pages (one in english and other in german), make a homepage duplicate by setting the homepage as a template for pages. My issue is the menu. How do I set different menus to different pages?


Solution

  • You'll need to register a new navigation menu, and add it to the templates you are using for the German pages, then make a new header template, then set your templates to use that new header. Here's the steps needed to do this:

    Register the menu

    In your theme's functions.php, you'll need to add some code like this at the end of the file:

    function register_german_menu() {
      register_nav_menu('german_menu',__( 'German Menu' ));
    }
    add_action( 'init', 'register_german_menu' );
    

    What this does is tells Wordpress that you want to add a new location for a menu to be customised in the Appearance > Menus page. You should from now be able to add items to your menu, but they won't show up anywhere yet.

    Create some page templates

    You will have to create a set of page templates for the German half of the site. You will need a new page.php and a new header.php template. I would recommend calling these page-german.php and header-german.php respectively. For your page-german.php, copy paste the page.php template into page-german.php, and add this comment at the top of page-german.php:

    <?php /* Template Name: German Page Template */ ?>
    

    This tells WordPress to recognise it as a page template, and tells it it's name. This makes the template selectable on each individual page.

    Now, you need to make a header template. Copy paste the header.php template into the header-german.php. In this new template, look for a line that looks something like

    wp_nav_menu(array('theme_location'=>'something'))
    

    And change it to:

    wp_nav_menu(array('theme_location'=>'german_menu'))
    

    This will display the German menu instead of the regular menu.

    Now, go back to page-german.php and look for the line that looks like:

    <?php get_header(); ?>
    

    And change it to:

    <?php get_header('german'); ?>
    

    This tells Wordpress that on this page, you want to use the header-german.php file to generate the header rather than header.php.

    Setting up page templates and Menus

    First, go to the Appearance > Menus tab, and create a new menu, then assign that new menu to the slot "German Menu". Then, you will need to go through all the German pages of your site, and in the right sidebar when editing, set the page template to "German Page Template". Now your pages using the new template will display the German menu instead of the English one!

    Further?

    If you have other templates that need converting, then you can convert them as described above, by making a copy and changing the get_header() call. I didn't really expect this answer to be this long but I hope it helps!