Set body class depending on language (4 posts)

  1. namoras
    Posted 4 years ago #

    I don't know if this was discussed before (found nothing), but since I now spent a few ours on it, I probably want to know how I did it later on ;) and other might be interested, I put it here.

    My theme (Suffusion) does the header image with CSS background-image:, so I needed a different class for each language to be able to CSS-define a different header image in each language. I now put the a class named [language-slug] into the body tag using the body_class function of WordPress. I put the following in the functions.php of my theme child:

    add_filter('body_class', 'my_custom_body_class', 10, 2);
      function my_custom_body_class($classes) {
        $classes[] = get_bloginfo('language');
        return $classes;

    This adds the class name 'de-DE', 'fr-FR' or 'it-IT' to the class list of the body tag, depending on the current language. I now can define the CSS background-image (and of cours other stuff) depending on the language, i.e.:

    body.de-DE #header-container {
      background-image: url(wp-content/themes/suffision-child/images/header-pic-de.png);
    body.fr-FR #header-container {
      background-image: url(wp-content/themes/suffision-child/images/header-pic-fr.png);
    body.it-IT #header-container {
      background-image: url(wp-content/themes/suffision-child/images/header-pic-it.png);


  2. Chouby
    Plugin Author

    Posted 4 years ago #

    Thank you for sharing !

  3. marlora
    Posted 4 years ago #

    Thank you, namoras!
    could please explain to a newbie how to add the language_slug using the body_class function? I have no clue of css and being able to change header according to the language is really important for my website.
    Thank you in advance!

  4. ChowKaiDeng
    Posted 4 years ago #

    Hi! I think that this solution will be easier for what marlora is asking.

    It just work with the templates pages, not the functions or styles.


Topic Closed

This topic has been closed to new replies.

About this Plugin

  • Polylang
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic