描述:今天分享PC电脑端和移动手机端显示不同内容的方法:一:在 functions.php 中加入如下代码:// 判断PC端function is_pc() { $user_agent = $_SERVER[HTTP_USER_AGENT];…
今天分享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()这段代码,该代码会自动判断访客客户端是否是移动端,如果是移动端则显示,不是移动端则不显示;
pbootcms内核开发的网站模板,该模板适用于微信小程序网站、软件开发网站等企业,当然其他行业也可以做,只需要把文字图片换成其他行业的即可;PC+WAP,同一个后台,数据即时同步,简单适用!附带测试数据!友好的seo,所有页面均都能完全自定义标题/关键词/描述,PHP程序,安全、稳定、快速;用低成…
(PC+WAP)粉色家政服务公司网站模板 月嫂保姆网站源码下载pbootcms内核开发的网站模板,该模板适用于家政服务公司网站、月嫂保姆网站等企业,当然其他行业也可以做,只需要把文字图片换成其他行业的即可;PC+WAP,同一个后台,数据即时同步,简单适用!附带测试数据!友好的seo,所有页…
(PC+WAP)红色火锅加盟网站pbootcms模板 餐饮美食网站源码下载pbootcms内核开发的网站模板,该模板适用于火锅加盟网站、餐饮美食网站等企业,当然其他行业也可以做,只需要把文字图片换成其他行业的即可;PC+WAP,同一个后台,数据即时同步,简单适用!附带测试数据!友好的seo,所有页面…
(PC+WAP)绿色草坪地坪施工pbootcms网站模板 操场人造草坪网站源码下载pbootcms内核开发的网站模板,该模板适用于人造草坪网站、草坪地坪网站等企业,当然其他行业也可以做,只需要把文字图片换成其他行业的即可;PC+WAP,同一个后台,数据即时同步,简单适用!附带测试数据!友好的seo,…
本套织梦模板采用织梦最新内核开发的模板,这款模板使用范围广,不仅仅局限于一类型的企业,政府部门、单位机构类的网站都可以用该模板。你只需要把图片和文章内容换成你的即可,颜色都可以修改,改完让你耳目一新的感觉!布局规整,利于用户体验,手工书写DIV+CSS,代码精简。同一个后台管理 三网合一简…
1.更新下载类目的采集 及 导入网站程序:jizhiCMS + 高仿新版某刀资源网模板采集程序:蓝天采集 + 已对接好资源站规则网站图片及蓝天采集规则安装教程在下面⬇⬇⬇⬇⬇⬇⬇ 链接:https:/…