重定向从桌面访问移动网站的用户

Redirect users visiting mobile site from desktop

我使用 Wordpress 创建了 2 个不同的网站,一个用于移动设备,一个用于桌面设备!我使用了一个名为等效移动重定向的插件,以便在移动用户访问桌面网站时将他们重定向到移动网站!现在我需要反之亦然,但我似乎找不到有效的方法!有什么想法吗?

您可以通过javascript查询:

var isMobile = {
    Android: function() {
        return navigator.userAgent.match(/Android/i);
    },
    BlackBerry: function() {
        return navigator.userAgent.match(/BlackBerry/i);
    },
    iOS: function() {
        return navigator.userAgent.match(/iPhone|iPad|iPod/i);
    },
    Opera: function() {
        return navigator.userAgent.match(/Opera Mini/i);
    },
    Windows: function() {
        return navigator.userAgent.match(/IEMobile/i);
    },
    any: function() {
        return (isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() || isMobile.Opera() || isMobile.Windows());
    }
};

if(isMobile.any()){
    // Mobile!
} else {
    // Desktop
}

如果您不想使用 Javascript,请查看此代码。如果访问者使用桌面浏览器,您可以使用 WordPress 的检测移动功能进行重定向。

if(!wp_is_mobile()){
    // If not using mobile
    wp_redirect( "https://your_desktop_site.com");
    exit;
}

您可以将此代码添加到主题的 functions.php 文件中,它会起作用。