首页 > 解决方案 > 如果翻译不存在,则显示“此页面未翻译”。WP 和 WPML

问题描述

好吧,我有一个带有 WPML 的 WordPress 网站。并非我所有的页面都翻译成英文版。我知道要实现跟随。当有人尝试访问不存在的 URL 时,我想向他/她显示“未翻译页面”(建议以另一种语言观察此内容)。我怎样才能意识到它?谢谢!

标签: wordpressredirecttranslationwpml

解决方案


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();
    }

}


推荐阅读