WordPress 根据访问设备使用不同的主题

根据访问设备的不同,区分开浏览器版本和PC与移动端,代码如下:

function ws_switch_theme($theme){
    global $is_IE;
    if($is_IE){
        preg_match('/MSIE\s(\d)\.0;/', $_SERVER['HTTP_USER_AGENT'], $matches);
        $IEversion = $matches[1];
        if($IEversion=6){
            $theme='twentyten';//IE 6 
        }
        if($IEversion=7){
            $theme='twentyeleven';//IE 7
        }
        if($IEversion=8){
            $theme='twentytwelve';//IE 8
        }
    }
    if(wp_is_mobile()) {
        $theme='twentytwelve';//移动端
    }
    return $theme;
}
add_filter( 'template', 'ws_switch_theme' );
add_filter( 'stylesheet', 'ws_switch_theme' );

你可以根据上面的代码自行修改,在何种设备访问时加载什么主题。注意主题名字一定是主题文件夹名字,而不是后台管理界面你看到的主题名字。
请勿直接添加到主题的 functions.php 中,无法生效。

使用方法:

1.在 Code Snippets 插件 中添加此段代码
插件下载地址:https://tw.wordpress.org/plugins/code-snippets/

2.把代码写成插件,安装!(反正我是不会,有能力的自己去写吧!)

(0)

相关推荐