如何在WordPress中获取访问类型?

时间:2016-01-15 作者:SparkAndShine

如何在WordPress中获取访问类型(从桌面或移动设备)?

这里发布了一个可能的解决方案here.

function yundanran_get_access_type()
{
    $is_wap=false;

    $user_agent = $_SERVER[\'HTTP_USER_AGENT\'];
    $mobile_agents = Array
    (
        "240x320","acer","acoon","acs-","abacho","ahong","airness",
        "alcatel","amoi","android","anywhereyougo.com","applewebkit/525",
        "applewebkit/532","asus","audio","au-mic","avantogo",
        "becker","benq","bilbo","bird","blackberry","blazer","bleu",
        "cdm-","compal","coolpad","danger","dbtel","dopod","elaine",
        "eric","etouch","fly ","fly_","fly-","go.web","goodaccess",
        "gradiente","grundig","haier","hedy","hitachi","htc",
        "huawei","hutchison","inno","ipad","ipaq","ipod","jbrowser",
        "kddi","kgt","kwc","lenovo","lg ","lg2","lg3","lg4","lg5",
        "lg7","lg8","lg9","lg-","lge-","lge9","longcos","maemo",
        "mercator","meridian","micromax","midp","mini","mitsu",
        "mmm","mmp","mobi","mot-","moto","nec-","netfront","newgen",
        "nexian","nf-browser","nintendo","nitro","nokia","nook",
        "novarra","obigo","palm","panasonic","pantech","philips",
        "phone","pg-","playstation","pocket","pt-","qc-","qtek",
        "rover","sagem","sama","samu","sanyo","samsung","sch-",
        "scooter","sec-","sendo","sgh-","sharp","siemens","sie-",
        "softbank","sony","spice","sprint","spv","symbian","tablet",
        "talkabout","tcl-","teleca","telit","tianyu","tim-","toshiba",
        "tsm","up.browser","utec","utstar","verykool","virgin","vk-",
        "voda","voxtel","vx","wap","wellco","wig browser","wii",
        "windows ce","wireless","xda","xde","zte"
    );
    $is_wap = false;
    foreach ($mobile_agents as $device) 
    {
        if (stristr($user_agent, $device)) 
        {
            $is_wap = true;
            break;
        }
    }

    return $is_wap?\'wap\':\'web\';
}
有更好的方法吗?

1 个回复
最合适的回答,由SO网友:C C 整理而成

这不是一个真正的WordPress问题。。。但与其重新发明车轮,不如看看this. 我在任何需要检测移动设备的网站上都使用它,而且效果很好。我在他们的页面上看到,他们甚至提到有使用此代码的WP插件。

相关推荐

Change menu for mobile

我正在WordPress网站上工作,我试图根据设备显示不同的菜单(桌面与移动)。为了做到这一点,我在标题中添加了一些代码。php文件:<?php if(wp_is_mobile()){ wp_nav_menu( array( \'menu\' => \'secon