document.writeln('');
今天分享PC電腦端和移動手機端顯示不同內容的方法:
一:在 functions.php 中加入如下代碼:
// 判斷PC端function is_pc() {$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 = ture;foreach ($mobile_browser as $device) {if (stristr($user_agent, $device)) {$is_mobile = false;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 = ture;break;}}return $is_mobile;}// 判斷PC端 function is_pc() { $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 = ture; foreach ($mobile_browser as $device) { if (stristr($user_agent, $device)) { $is_mobile = false; 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 = ture; break; } } return $is_mobile; }// 判斷PC端function is_pc() {$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 = ture;foreach ($mobile_browser as $device) {if (stristr($user_agent, $device)) {$is_mobile = false;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 = ture;break;}}return $is_mobile;}
二:WordPress模板中調用
在WP模板中調用,如 index.php、footer.php、single.php 等,調用方式:
1、在PC電腦端顯示,移動端不顯示
<?php if (is_pc() ): ?>
<div>
<p>我在PC電腦端顯示</p>
</div>
<?php endif ;?>
2、在移動端顯示,PC端不顯示
<?php if (is_mobile() ): ?>
<div>
<p>我在移動端顯示,不在PC端顯示</p>
</div>
<?php endif ;?>
綜上,第一步在主題下的functions.php文件中插入代碼,然后再WordPress主題模板中調用即可。
總結:
該代碼的主要解決的問題自動判斷用戶的客戶端,然后站長根據不同的客戶端來展示不同的內容。
使用is_pc()這段代碼,該代碼會自動判斷訪客客戶端是否是PC,如果是PC則顯示,不是PC則不顯示;
使用is_mobile()這段代碼,該代碼會自動判斷訪客客戶端是否是移動端,如果是移動端則顯示,不是移動端則不顯示;