首页 > 解决方案 > 如何在 PolyLang 插件中切换特定语言?

问题描述

有没有办法像 WPML 一样在 PolyLang 插件中切换语言?例如:

全球 $sitepress; $sitepress->switch_lang($lang);

标签: wordpresspolylang

解决方案


有关 polylang 插件功能的更多信息,请访问插件文档

如果无法使用 Polylang,那么您可以使用默认的 WordPress 过滤器,如下所示:

// Set user selected language by loading the lang.mo file
if ( is_user_logged_in() ) {

    // add local filter
    add_filter('locale', 'language');

    function language($locale) {
        /* Note: user_meta and user_info are two functions made by me,
           user_info will grab the current user ID and use it for
           grabbing user_meta */

        // grab user_meta "lang" value
        $lang = user_meta(user_info('ID', false), 'lang', false); 

        // if user_meta lang is not empty
        if ( !empty($lang) ) {
           $locale = $lang; /* set locale to lang */
        }

        return $locale; 
    }

    // load textdomain and .mo file if "lang" is set
    load_theme_textdomain('theme-domain', TEMPLATEPATH . '/lang');

}

推荐阅读