有段代码需要在手机上显示,而电脑上不用,一开始准备用js,但是突然想起在一款主题里面看见过现成的代码,就直接用下。

把代码放到主题的functions.php文件:

/*移动判断*/
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() ): ?>
    手机端显示代码
<?php endif ;?>

代码来源Loostrive主题,这款主题是我见过最值得购买的国产主题了,36还是38元,我买很多年了。

主题功能不多,但是基础够用了。