wordpress - 如果翻译不存在,则显示“此页面未翻译”。WP 和 WPML
问题描述
好吧,我有一个带有 WPML 的 WordPress 网站。并非我所有的页面都翻译成英文版。我知道要实现跟随。当有人尝试访问不存在的 URL 时,我想向他/她显示“未翻译页面”(建议以另一种语言观察此内容)。我怎样才能意识到它?谢谢!
解决方案
add_action('wp_head', 'wpml_custom');
function wpml_custom() {
global $wp_query;
$postId = $wp_query->post->ID;
$postType = $wp_query->post->post_type;
$args = [
'element_id' => $postId, 'element_type' => $postType
];
$translation = apply_filters( 'wpml_element_language_details', null, $args );
$currentLang = apply_filters( 'wpml_current_language', null );
if (preg_match('/^\/$/', $_SERVER['REQUEST_URI'])) {
return;
} elseif ($translation->language_code !== $currentLang) {
require get_template_directory() . '/no-translation.php';
exit();
}
}
推荐阅读
- c++ - CLion 中的 Boost 安装
- reactjs - 如何在颜色选择器中使用所选颜色制作 div
- spring-boot - 带有 Spring-boot 后端的 Flutter websocket
- java - 我向wordpress rest api发送请求以获取令牌,但我得到了奇怪的回调
- c# - 更改硒中的书签
- xml - 在 XSL 中创建可点击的外部链接以创建 PDF
- c++ - 变量引用以及指针如何与内存交互
- javascript - 嵌套 if 语句 - 如何在迭代时重构条件以使用一个 if 语句
- azure - 使用 Azure 函数从 Azure Blob 存储中删除超过 X 天的文件
- c# - NSwag 缺少 C# 客户端生成代码的 [Display] 数据注释