document.writeln('');
今天分享PC電腦端和移動(dòng)手機(jī)端顯示不同內(nèi)容的方法:
一:在 functions.php 中加入如下代碼:
// 判斷PC端function is_pc() {$user_agent = $_SERVER[HTTP_USER_AGENT];$mobile_browser = Array("mqqbrowser", //手機(jī)QQ瀏覽器"opera mobi", //手機(jī)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;}// 判斷手機(jī)端function is_mobile() {$user_agent = $_SERVER[HTTP_USER_AGENT];$mobile_browser = Array("mqqbrowser", //手機(jī)QQ瀏覽器"opera mobi", //手機(jī)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", //手機(jī)QQ瀏覽器 "opera mobi", //手機(jī)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; } // 判斷手機(jī)端 function is_mobile() { $user_agent = $_SERVER[HTTP_USER_AGENT]; $mobile_browser = Array( "mqqbrowser", //手機(jī)QQ瀏覽器 "opera mobi", //手機(jī)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", //手機(jī)QQ瀏覽器"opera mobi", //手機(jī)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;}// 判斷手機(jī)端function is_mobile() {$user_agent = $_SERVER[HTTP_USER_AGENT];$mobile_browser = Array("mqqbrowser", //手機(jī)QQ瀏覽器"opera mobi", //手機(jī)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模板中調(diào)用
在WP模板中調(diào)用,如 index.php、footer.php、single.php 等,調(diào)用方式:
1、在PC電腦端顯示,移動(dòng)端不顯示
<?php if (is_pc() ): ?>
<div>
<p>我在PC電腦端顯示</p>
</div>
<?php endif ;?>
2、在移動(dòng)端顯示,PC端不顯示
<?php if (is_mobile() ): ?>
<div>
<p>我在移動(dòng)端顯示,不在PC端顯示</p>
</div>
<?php endif ;?>
綜上,第一步在主題下的functions.php文件中插入代碼,然后再WordPress主題模板中調(diào)用即可。
總結(jié):
該代碼的主要解決的問(wèn)題自動(dòng)判斷用戶的客戶端,然后站長(zhǎng)根據(jù)不同的客戶端來(lái)展示不同的內(nèi)容。
使用is_pc()這段代碼,該代碼會(huì)自動(dòng)判斷訪客客戶端是否是PC,如果是PC則顯示,不是PC則不顯示;
使用is_mobile()這段代碼,該代碼會(huì)自動(dòng)判斷訪客客戶端是否是移動(dòng)端,如果是移動(dòng)端則顯示,不是移動(dòng)端則不顯示;