ThemeShaper Forums » Thematic

[closed]

How to add a class to the <body> declaration

(6 posts)
  • Started 4 years ago by philipmjones
  • Latest reply from cannobbio
  • This topic is resolved
  1. I am creating multilingual sites and want to change styles based on what language a page is displayed in. Many browsers allow you to define styles that only apply to the specified language, eg. #page-top:lang(en). However IE7 and earlier and all versions of Safari ignore the lang declaration. So instead I would like to add a language specifier as a style in the body declaration. For example

    <body class="wordpress ..... lang-en"

    I have found the part of header.php that I need to modify in my child theme, inserting PHP code that extracts the language from my page/post, thanks to the WPML multilingual plugin.

    <body class="<?php thematic_body_class() ?> lang-<?php echo ICL_LANGUAGE_CODE?>">

    However I suspect that header.php is likely to change in future versions of Thematic, so would like to find a way of using a hook, filter or something upgrade safe that would allow me to make this addition to the body.

    Any suggestions, please?

    Posted 4 years ago #
  2. Hi,

    you can filter body_class. The following code will take the array used to build the body class, add your code and send it back:

    function extend_body_class($c) {
    	$c[] = 'lang-' . ICL_LANGUAGE_CODE;
    	return $c;
    }
    add_filter('body_class', 'extend_body_class');

    Chris

    Posted 4 years ago #
  3. Thanks Chris! :)

    Posted 3 years ago #
  4. cannobbio
    Member

    Hi Chris
    or anybody =)

    Is this filter working yet?
    It doesn't work for me.

    I'm using WP 3.1 , WPML 2.1.1 and Thematic 0.9.7.8 rev.762

    "ICL_LANGUAGE_CODE" is still on the WPML API:
    http://wpml.org/documentation/support/wpml-coding-api/

    Thank you!

    Posted 3 years ago #
  5. I can confirm that it's working perfectly for me.

    This morning I've been working on a new site with the latest versions of Wordpress and the latest, paid-for version of WPML. I cut and pasted Chris's code into my child theme's functions.php and it worked right away.

    So a definite YES and belated thanks to Chris for this perfect answer.

    Posted 3 years ago #
  6. cannobbio
    Member

    Got it!
    It didn't work for me, but it works now since I filtered "thematic_body_class" instead of "body_class".

    function extend_body_class($c) {
    	$c[] = 'lang-' . ICL_LANGUAGE_CODE;
    	return $c;
    }
    add_filter('thematic_body_class', 'extend_body_class');

    Thank you!!!

    Posted 3 years ago #

RSS feed for this topic

Topic Closed

This topic has been closed to new replies.