分享好友 资讯首页 资讯分类 切换频道

wordpress如何判断是否为手机移动设备

2025-07-27 22:0000admin001

现在手机移动设备越来越普及,也越来越智能,使用手机浏览网页已经比较流行了,所以,作为wordpress主题开发者,你必须好好考虑如何应对手机移动用户了。
这是一段php通用的判断移动浏览器的函数,原理比较简单,就是判断浏览器返回的user_agent,条件包括手机系统、品牌和窗口大小。
以wordpress为例,在主题的functions.php内加上如下代码,目前已包含常见移动浏览器的useragent,基本上可以涵盖可能会用手机上网的用户群了。

functionis_mobile(){	$user_agent=$_SERVER['HTTP_USER_AGENT'];	$mobile_browser=Array(		"mqqbrowser",//手机QQ浏览器		"operamobi",//手机opera		"juc","iuc",//uc浏览器		"fennec","ios","applewebKit/420","applewebkit/525","applewebkit/532","ipad","iphone","ipaq","ipod",		"iemobile","windowsce",//windowsphone		"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_browseras$device){		if(stristr($user_agent,$device)){			$is_mobile=true;			break;		}	}	return$is_mobile;}
举报
收藏 0
打赏 0
评论 0
dedecms怎么添加会员功能

0评论2025-07-3117

dedecms好操作吗

0评论2025-07-3117

WordPress织梦哪个快

0评论2025-07-3116

dedecms如何修模板

0评论2025-07-3116