这里是一个完整且更好的解决方案,可以只更改主题(前端)或插件语言。它甚至可以更改特定的页面/帖子等语言。
TLDR有一个名为locale
, 它确定要加载的语言。您可以根据自己喜欢的条件更改为特定语言。
Changing only Theme language, or based on other conditions, without touching the admin dashboard language.
add_filter(\'locale\', function($locale) {
if( is_admin() ) {
return $locale; // Leave default language if it is admin side.
}
// You can load language based on any conditional tags here, like, is_page(), is_singular(), is_archive() etc.
// I want to change the language for all front end in this particular example.
$locale = \'en_US\'; // Change to your required language. The language must be installed and loaded.
return $locale; // Return language to be used.
});
更改语言之前,可能需要先加载该语言。
在主题或子主题中,使用此代码段加载自定义语言。
add_action( \'after_setup_theme\', function() {
// Assuming your language files are in /wp-content/themes/your-theme/languages
// Don\'t forget to change \'text-domain\' to your theme text-domain.
// For Theme.
load_theme_textdomain( \'text-domain\', get_template_directory() . \'/languages\' );
// For Child Theme
load_child_theme_textdomain( \'text-domain\', get_stylesheet_directory() . \'/languages\' );
// Remove the load_child_theme_textdomain if you are using it for the main theme,
// OR Remove load_theme_textdomain if you are loading child theme language.
});
同样,您可以根据URL或插件页面等更改语言。
希望这会有帮助。如果我错过了什么,也可以告诉我。