document.writeln('');

国产精品最大AV站在线观看,a级黄毛片试看三分钟,AV无码A片高潮AV,国产午夜精品一区二区三区,久久爆乳不卡一区,日本三级欧美三级,日韩精品AV一二三区在线,高H短篇小说抽插

當前位置:首頁 > 免費源碼 > WordPre判斷PC端和移動端并顯示不同內容的方法

WordPre判斷PC端和移動端并顯示不同內容的方法

游戲科技網2026年09月14日 23:53免費源碼110

今天分享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()這段代碼,該代碼會自動判斷訪客客戶端是否是移動端,如果是移動端則顯示,不是移動端則不顯示;