欢迎来到361模板(www.ke361.com),原创精品织梦模板提供商。QQ快速注册登录 网站托管 VIP 网站定制

织梦模板

当前位置:织梦模板 > 技术文档 > CMS技术 > WordPress判断PC端和移动端并显示不同内容的方法
这篇文章主要为大家详细介绍了WordPress判断PC端和移动端并显示不同内容的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,有需要的朋友可以收藏方便以后借鉴。

    之前小编给大家介绍过《JS代码判断客户端是手机访问还是PC电脑访问》,有些童鞋就想了,是否可以实现不同客户端显示不同的内容呢?下面我们就WordPress来实现通过php通过http_user_agent即可判断用户的客户端。

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


今天织梦361分享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()这段代码,该代码会自动判断访客客户端是否是移动端,如果是移动端则显示,不是移动端则不显示;

以上就是WordPress判断PC端和移动端并显示不同内容的方法的全部内容,希望对大家的学习和解决疑问有所帮助,也希望大家多多支持361模板网。

感谢打赏,我们会为大家提供更多优质资源!

热词标签: WordPress 移动端 PC端

上一篇三种织梦dedecms友情链接调用标签

下一篇WordPress免插件生成完整站点地图(sitemap.xml)的php代码及实现方法

取消

感谢您的支持,我们会继续努力的!

扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

Powered by361模板网,分享从这里开始,精彩与您同在