【问题标题】:Wordpress changing themes based on user agentWordpress 基于用户代理更改主题
【发布时间】:2019-05-27 13:24:13
【问题描述】:

我创建了两个单独的主题。一种用于移动设备,一种用于台式机。我已经设法使用下面显示的功能更改主题。更改主题有效,但该函数仍会从原始主题和主主题加载 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');

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

【问题讨论】:

  • 您应该使用响应式样式,而不是单独的主题。
  • @rblarsen 由于两种设备的用户体验完全不同,这不是一个可行的选择
  • 您可能需要检查一次。 wordpress.stackexchange.com/a/115417/27998

标签: php html css wordpress


【解决方案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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多