这是一段php通用的判断移动浏览器的函数,原理比较简单,就是判断浏览器返回的user_agent,条件包括手机系统,品牌和窗口大小.
以wordpress为例,在主题的function.php内加上如下代码,我找了一些常见移动浏览器的useragent,其中有很多国内流行的手机浏览器,基本上可以涵盖可能会用手机上网的用户群了..
function is_mobile() { $user_agent = $_SERVER['HTTP_USER_AGENT']; $mobile_browser = Array( "mqqbrowser", //手机QQ浏览器 "opera mobi", //手机opera "juc","iuc",//uc浏览器 "fennec","ios","applewebKit/420","applewebkit/525","applewebkit/532","ipad","iphone","ipaq","ipod", "iemobile", "windows ce",//windows phone "240x320","480x640","acer","android","anywhereyougo.com","asus","audio","blackberry","blazer","coolpad" ,"dopod", "etouch", "hitachi","htc","huawei", "jbrowser", "lenovo","lg","lg-","lge-","lge", "mobi","moto","nokia","phone","samsung","sony","symbian","tablet","tianyu","wap","xda","xde","zte" ); $is_mobile = false; foreach ($mobile_browser as $device) { if (stristr($user_agent, $device)) { $is_mobile = true; break; } } return $is_mobile; }
然后在主题任意模板如顶部加上如下判断:
<?php if (is_mobile() ): ?> 怎样怎样..(这里可以添加一个mobile.css,如<link rel="stylesheet" type="text/css" media="all" href="<?php echo get_template_directory_uri(); ?>/mobile.css" />) <?php endif ;?>
关于制作移动设备主题的注意事项我会在以后更新...
mobile-hack is coded by http://xiaohudie.net. Any posts here is one hundred percent original, so please keep my link so as not to hurt this pretty girl. -
更新了没?加载另一个主题代码是什么?
求教博主怎么判断手机后加载另一个主题。
@WindyLiu 换主题要用插件 因为在主题里写函数不能直接调用别的主题
@WindyLiu 我一般的处理方式是通过浏览器判断是否为移动设备,然后根据此来判断选择加载不同的文件。
是客户端的吧?
不过用判断还是挺好的,如果我在手机里不想展示的模块如果来隐藏?不通过CSS来做
@ilikecss 这篇是很早以前写的了,判断手机ua还可以用wp_is_mobile,通过php实现不想展示的就不输出
我用什么浏览器回复你可以看出来
@vow 你牛!服了
不错,很好的判断函数,现在mobile函数应该可以直接判断了