黑海洋Wiki | AI机器人硬件开发 | 网络安全攻防实战 | 区块链技术文档教程 - 免费资源平台 - 免插件
type: channel
link: https://blog.upx8.com/tag/%E5%85%8D%E6%8F%92%E4%BB%B6/
items: 利用wordpress代码判断移动设备

利用wordpress代码判断移动设备
type: item
date: 2014-06-11T15:40:25+08:00
description: .encrypted-content { background: linear-gradient(135deg, #fdfeff, #f3f9ff); padding: 20px; border: 2px dashed #76c7e6; border-radius: 12px; margin: 15px 0; box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1); color: #007bff; text-align: center; position: relative; overflow: hidden; transition: all 0.3s ease; } .encrypted-content:before { content: "🐸🤔 🤐 😄 🤣 😂 😝 🤑"; position: absolute; top: -10px; left: 50%; transform: translateX(-50%); font-size: 30px; color: #007bff; opacity: 0.2; z-index: 0; } .encrypted-content:hover { background: linear-gradient(135deg, #d6ecff, #cce8ff); border-color: #54aed8; transform: scale(1.02); box-shadow: 0 6px 10px rgba(0, 0, 0, 0.15); } .encrypted-content span { display: inline-block; position: relative; z-index: 1; animation: fadeIn 1.5s infinite; } @keyframes fadeIn { 0%, 100% { opacity: 0.6; } 50% { opacity: 1; } }这是一段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" />) <?p文件URL>