WordPress根据用户代理更改主题

时间:2019-05-27 13:24:13

标签: php html css wordpress

我创建了两个单独的主题。一种用于移动设备,一种用于台式机。我设法通过下面显示的功能更改了主题。更改主题是可行的,但是该功能仍会从原始主题和主要主题加载CSS,HTML,JS和PHP代码。

桌面主题和移动主题都具有相同的文件结构和链接。

文件夹结构如下

project
      |
      wp_content
               |
               themes
                    |
                    desktop
                    mobile
function change_theme($current_theme)
{
    $user_agent = $_SERVER['HTTP_USER_AGENT'];
    if (strpos($user_agent, 'CostumizedAgent') !== false) {
        return 'mobile';
    } else {
        return $current_theme;
    }
}
add_filter('stylesheet', 'change_theme');
add_filter('template', 'change_theme');

我希望加载主题的实际文件,而不是主主题的文件。有什么建议吗?

1 个答案:

答案 0 :(得分:1)

尝试一下。 wp_is_mobile()函数有助于检测设备

add_filter('body_class','mobile_theme_body_class');     
function mobile_theme_body_class( $classes ){
    if ( wp_is_mobile() ){
        return 'mobile';
    } else {
        return 'desktop';
    }
    return $classes;
}
相关问题