现在手机移动设备越来越普及,也越来越智能,使用手机浏览网页已经比较流行了,所以,作为WordPress主题开发者,你必须好好考虑如何应对手机移动用户了。
应对手机用户的主要策略
1.制作自适应WordPress主题
2.额外制作一个专供手机用户访问时显示的主题,或者使用相关手机主题设置插件
如果你打算制作针对手机移动用户的WordPress主题,可能就需要判断手机移动客户端。昨天在 小蝴蝶 那里看到了 wordpress免插件判断移动设备 ,与大家一起分享。
WordPress判断手机移动设备
这是一段php通用的判断移动浏览器的函数,原理比较简单,就是判断浏览器返回的user_agent,条件包括手机系统、品牌和窗口大小。
以WordPress为例,在主题的 functions.php 内加上如下代码,目前已包含常见移动浏览器的useragent,基本上可以涵盖可能会用手机上网的用户群了。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | 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; } |
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; }
然后在主题任意模板如顶部加上如下判断:
1 2 3 | <?php if (is_mobile() ): ?> //怎样怎样..(这里可以添加一个mobile.css,如<link rel="stylesheet" type="text/css" media="all" rel="nofollow noopener noreferrer" href="<?php echo get_template_directory_uri(); ?>/mobile.css" />) <?php endif ;?> |
<?php if (is_mobile() ): ?> //怎样怎样..(这里可以添加一个mobile.css,如<link rel="stylesheet" type="text/css" media="all" rel="nofollow noopener noreferrer" href="<?php echo get_template_directory_uri(); ?>/mobile.css" />) <?php endif ;?>
还需要注意的一点:不管是单独的WordPress主题还是自适应主题,都需要在头部<head>将添加下面meta,否者可能导致手机显示字体过小等问题。
1 | <meta name="viewport" content="width=device-width"/> |
<meta name="viewport" content="width=device-width"/>
参考资料:http://xiaohudie.net/mobile-hack.html
本文WordPress主题制作 之 判断手机移动设备到此结束。每个成功者都有一个开始。勇于开始,才能找到成功的路。小编再次感谢大家对我们的支持!