WordPress判断PC电脑端和移动手机端显示不同内容

WordPressWordPress判断PC电脑端和移动手机端显示不同内容已关闭评论21

判断用户PC电脑端和手机移动端,不同客户端显示不同的内容是WordPress必不可少的功能,通过php通过http_user_agent即可判断用户的客户端,新手站长网分享PC电脑端和移动手机端显示不同内容的方法:

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

找到你的WordPress主题中的functions.php文件,在functions.php中插入以下代码:文章源自新手站长-https://xinshouzhanzhang.com/wppcmobile.html

// 判断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电脑端显示,移动端不显示文章源自新手站长-https://xinshouzhanzhang.com/wppcmobile.html

<?php if (is_pc() ): ?>
	<div>
    <p>我在PC电脑端显示</p>
	</div>
<?php endif ;?>

2、在移动端显示,PC端不显示文章源自新手站长-https://xinshouzhanzhang.com/wppcmobile.html

<?php if (is_mobile() ): ?>
	<div>
    <p>我在移动端显示,不在PC端显示</p>
	</div>
<?php endif ;?>

综上,第一步在主题下的functions.php文件中插入代码,然后再WordPress主题模板中调用即可。文章源自新手站长-https://xinshouzhanzhang.com/wppcmobile.html 文章源自新手站长-https://xinshouzhanzhang.com/wppcmobile.html

【阿里云99元服务器】入口2核2G3M带宽、新老用户同享、99元/年续费到2027年

【腾讯云特价】:2核2G3M 88元一年、2核2G4M 108元1年 366元3年、2核4G5M 166元/年 566元3年、4核8G12M 446元一年 更多配置8核16G18M和16核32G28M 点我直达 >>

2023阿里云优惠活动:xinshouzhanzhang.com/go/aliyun

注意:以上特价轻量服务器限制条件为“产品首单特惠”,如果你的腾讯云账号已经是老用户,建议重新注册一个腾讯云账号,如果你是新用户符合条件,那么无脑入,确实值得。

 
  • WordPress 移动
  • WordPress手机登录
  • WordPress手机访问
  • WordPress移动电脑自适应
  • WordPress自适应