Forums

Useful trick : Add your language attribute as a body class (1 post)

  1. TWD
    Member
    Posted 5 months ago #

    If anyone is running a multiligual blog and trying to work out
    how to show certain bits of content on some pages but not others
    depending on the current language of the page, this simple trick might help.

    By default in your "header.php" file (it's in your theme directory) uses this piece of code to output certain classes attached to the body element of your html

    <body <?php if(function_exists('body_class')) body_class(); ?>>

    Here is the cool part. Just amend the same line of code as follows:

    <body <?php if(function_exists('body_class')) body_class(ICL_LANGUAGE_CODE); ?>>

    By passing ICL_LANGUAGE_CODE to the body_class function as a variable,
    it will now include the official language code as per your DOCTYPE as a class name

    e.g. if your current page is in English, it will add the class 'en'
    e.g. if your current page is in Japanese, it will add the class 'ja'

    This assumes that you have set your WordPress language variables correctly. There are several plugins that do this. The most popular being WPML.

Reply

You must log in to post.

About this Topic