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

导读

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

// 手机访问需要显示的内容 www.mcbke.com  https://www.mcbke.com/pcyddxx.html ‎
function wpjam_is_mobile() {
    if ( empty($_SERVER['HTTP_USER_AGENT']) ) {
    return false;
    } elseif ( ( strpos($_SERVER['HTTP_USER_AGENT'], 'Mobile') !== false && strpos($_SERVER['HTTP_USER_AGENT'], 'iPad') === false) // many mobile devices (all iPh, etc.)
    || strpos($_SERVER['HTTP_USER_AGENT'], 'Android') !== false
    || strpos($_SERVER['HTTP_USER_AGENT'], 'Silk/') !== false
    || strpos($_SERVER['HTTP_USER_AGENT'], 'Kindle') !== false
    || strpos($_SERVER['HTTP_USER_AGENT'], 'BlackBerry') !== false
    || strpos($_SERVER['HTTP_USER_AGENT'], 'Opera Mini') !== false
    || strpos($_SERVER['HTTP_USER_AGENT'], 'Opera Mobi') !== false ) {
    return true;
    } else {
    return false;
    }
    }
// 手机访问不需要显示的内容 www.mcbke.com  https://www.mcbke.com/pcyddxx.html ‎
function E_is_mobile() {
    if ( empty($_SERVER['HTTP_USER_AGENT']) ) {
    return true;
    } elseif ( ( strpos($_SERVER['HTTP_USER_AGENT'], 'Mobile') !== false && strpos($_SERVER['HTTP_USER_AGENT'], 'iPad') === false) // many mobile devices (all iPh, etc.)
    || strpos($_SERVER['HTTP_USER_AGENT'], 'Android') !== false
    || strpos($_SERVER['HTTP_USER_AGENT'], 'Silk/') !== false
    || strpos($_SERVER['HTTP_USER_AGENT'], 'Kindle') !== false
    || strpos($_SERVER['HTTP_USER_AGENT'], 'BlackBerry') !== false
    || strpos($_SERVER['HTTP_USER_AGENT'], 'Opera Mini') !== false
    || strpos($_SERVER['HTTP_USER_AGENT'], 'Opera Mobi') !== false ) {
    return false;
    } else {
    return true;
    }
    }

使用方法

手机访问显示,电脑不显示;


//手机访问显示,电脑不显示内容
 

手机访问不显示,电脑显示;


//手机访问不显示,电脑显示内容;

温馨提示:本文最后更新于2020-12-01 16:56:02,某些文章具有时效性,若有错误或已失效,请在下方留言或联系MC百科网
© 版权声明
THE END
喜欢就支持一下吧
点赞0
分享
评论 抢沙发
MC百科的头像-MC百科

昵称

取消
昵称表情代码图片