wordpress - 如何在 PolyLang 插件中切换特定语言?
问题描述
有没有办法像 WPML 一样在 PolyLang 插件中切换语言?例如:
全球 $sitepress; $sitepress->switch_lang($lang);
解决方案
有关 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');
}
推荐阅读
- r - 将列中的 NA 值更改为前面的值,直到出现其他二进制数
- java - 正则表达式将引号与最少的单词数匹配
- windows - 如何使用 Batch For Loop 执行 ffmpeg
- reactjs - 如何使用 redux-saga 在 getInitialProps 中获取数据。然后在 getInitialProps 方法中从 redux store 获取响应?
- python - 如何在 Django 上模拟关系
- amazon-web-services - 如何使用 AWS CDK 添加 S3 BucketPolicy?
- electron - 无法将保存的页面添加到菜单栏 - 使用电子的包装应用程序
- python - 有没有办法在 scikit-learn MLPClassifier 上使用 Nadam 优化器?
- html - 单击按钮时如何仅更改数组?
- sql - 如何回滚更新表的存储过程