判断用户PC电脑端和手机移动端,不同客户端显示不同的内容是WordPress必不可少的功能,通过php通过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;}
二: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()这段代码,该代码会自动判断访客客户端是否是移动端,如果是移动端则显示,不是移动端则不显示;
让足球滚一会提供足球、篮球、NBA赛事前瞻分析推荐,与各联赛时实新闻报道、球员转会消息、赛事录像回放等资讯,用心认真把每件事做到最好的网站。
体育即时比分网提供实时足球比分与篮球比分数据,包括即时比分、赛程、球队、竞猜等数据,让您无时无刻都能掌握时实足球比分与篮球比分动态消息。即时更新各项比赛数据与完赛结果。让足球滚一会带您体验精彩的竞猜足球比赛!
金魔网
外籍模特
:imtoken钱包为您提供最新的imtoken钱包信息,imtoken是全球最大的数字货币钱包,支持包括BTC, ETH, BSC, TRON, Aptos, Polygon, Solana, OKExChain, Polkadot, Kusama, EOS等在内的所有主流公链及Layer 2,已为全球近千万用户提供可信赖的数字货币资产管理服务,也是当前DeFi用户必备的工具钱包。
我们专注提供明星代言、商演、翻包视频、祝福视频录制等业务,十多年行业服务经验