Skip to content

Instantly share code, notes, and snippets.

@wuliupo
Last active August 29, 2015 13:57
Show Gist options
  • Save wuliupo/9701055 to your computer and use it in GitHub Desktop.
Save wuliupo/9701055 to your computer and use it in GitHub Desktop.
wordpress language switch, no refesh, cookie remember;多语言切换,不刷新页面,cookie 记住当前语言
<?php
// /wp-content/themes/****/functions.php
add_filter( 'body_class', 'lang_body_classes' );
function lang_body_classes( $classes ) {
if($_GET['lang']=='en' || $_COOKIE['lang']=='en'){
$classes[] = 'en';
@setcookie('lang', 'en');
}
return $classes;
}
?>
<!-- /wp-content/themes/****/header.php -->
<div class="lang"><span data-lang="cn">中文</span> <span data-lang="en">English</span></div>
<script type="text/javascript">
jQuery('.lang span').click(function(){
var lang=jQuery(this).attr('data-lang');
document.cookie='lang='+lang;
jQuery('body').removeClass('cn en').addClass(lang);
});
</script>
<style type="text/css">
.en, body.en .cn{display: none;}
body.en, body.en .en{display: block;}
</style>
@wuliupo
Copy link
Author

wuliupo commented Mar 22, 2014

Post Content, 正文内容应该包含两套标签:
Any HTML tag (span, p, or div),

<tag class="cn">中文内容</tag>
<tag class="en">Chinese Content</tag>

remove all filters 移除对应的 HTML 标签过滤函数, esc_attr, esc_url, esc_attr_e, esc_html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment