WordPress判断PC端和移动端并显示不同内容的方法

WordPress判断PC端和移动端并显示不同内容的方法

今天分享PC电脑端和移动手机端显示不同内容的方法:

一:在 functions.php 中加入如下代码:

// <a target="_blank" href="https://www.srdhw.com/" title="View all posts in 判断">判断</a>PC端
function is_<a target="_blank" href="https://www.srdhw.com/" title="View all posts in pc">pc</a>() {
                $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()这段代码,该代码会自动判断访客客户端是否是移动端,如果是移动端则显示,不是移动端则不显示;

版权声明 1、本网站名称:俗人导航网
2、本站永久网址:https://www.srdhw.com/
3、本网站的文章部分内容可能来源于网络,仅供大家学习与参考,如有侵权,请联系站长微信:SuRen-WL进行删除处理。
4、本站一切资源不代表本站立场,并不代表本站赞同其观点和对其真实性负责。
5、本站一律禁止以任何方式发布或转载任何违法的相关信息,访客发现请向站长举报
6、本站资源大多存储在云盘,如发现链接失效,请联系我们我们会第一时间更新。
版权声明:俗人网络科技 发表于 2022年9月20日 pm9:59。
转载请注明:WordPress判断PC端和移动端并显示不同内容的方法 | 俗人导航网

相关文章