naro

您所在的位置:首页 > 解决方案 > 安装配置 > 正文
phpcms电脑和手机双模版 发表时间:2021-04-27 14:11:19 点击:
导言:记得之前联盟做双模版废了好大的劲儿,尝试了好多种方案,但没有记录下来,这次准备把blog做成双模版,顺便记录一下创作过程

服务器方面,因为在用免费的https,只支持单域名,所以pass掉解析 m.域名.com 的方案,电脑和手机都访问的 www.域名.com  。


step.1.

找到 /phpcms/libs/functions/global.func.php 文件,新增isMobile函数

//双模板
function isMobile() {  $useragent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
$useragent_commentsblock = preg_match('|\(.*?\)|', $useragent, $matches) > 0 ? $matches[0] : '';
function CheckSubstrs($substrs, $text) {
foreach ($substrs as $substr)
if (false !== strpos($text, $substr)) {
return true;
}
return false;
}$mobile_os_list = array('Google Wireless Transcoder', 'Windows CE', 'WindowsCE', 'Symbian', 'Android', 'armv6l', 'armv5', 'Mobile', 'CentOS', 'mowser', 'AvantGo', 'Opera Mobi', 'J2ME/MIDP', 'Smartphone', 'Go.Web', 'Palm', 'iPAQ');
$mobile_token_list = array('Profile/MIDP', 'Configuration/CLDC-', '160×160', '176×220', '240×240', '240×320', '320×240', 'UP.Browser', 'UP.Link', 'SymbianOS', 'PalmOS', 'PocketPC', 'SonyEricsson', 'Nokia', 'BlackBerry', 'Vodafone', 'BenQ', 'Novarra-Vision', 'Iris', 'NetFront', 'HTC_', 'Xda_', 'SAMSUNG-SGH', 'Wapaka', 'DoCoMo', 'iPhone', 'iPod');
$found_mobile = CheckSubstrs($mobile_os_list, $useragent_commentsblock) || CheckSubstrs($mobile_token_list, $useragent);
if ($found_mobile) {
return true;
} else {
return false;
}
}

WechatIMG109.jpeg


step.2.

找到 /phpcms/modules/content/index.php 文件,

在里面找到代码 include template('content','index',$default_style); 调整为

if (isMobile()){
    $default_style=$default_style."_m";
}else{
    $default_style=$default_style;
}
include template('content','index',$default_style);

找到 include template('content',$template); 有三处,调整为

$sitelist = getcache('sitelist', 'commons');
    if (isMobile()) {
        include  template('content', $template, $sitelist[$siteid]['default_style'] . '_wap');
    } else {
        include  template('content', $template);
    }

3.png


step.3.

完成以上步骤后就可以在 /phpcms/template/ 下建电脑和手机双模板了,比如电脑模板naro,手机则为naro_wap。