分享

phpcms 手机版本m. 电脑版www. 双模板操作 – 黄青在线

 梁方贵网络收藏 2015-08-28

移动互联网越来越兴起了!
现在大多网站都要开始适配移动版的网页了
例如,大部分人做站,希望电脑版为http://www.,手机版为http://m.
但是,总不能为了移动版.专门做一套站吧!
接下来就附上,phpcms中.自适配的代码.
首先,源文件里 modules/content/index.php
把 include template('content',$template);
改为
if(substr($_SERVER['SERVER_NAME'], 0,1) == 'm'){
include template('content_m',$template);
}else{
include template('content',$template);
}
以上代码的意思,当前页面url中.如果url中,第一个字符为m,则调用content_m模板,否则调用 content 模板
接着,就是一个问题.
由于 phpcms 把文章的url都固定写死在数据表中.所以,页面中的标签不能在使用{$r[url]}
而要改成{str_replace('http://www.','http://m.',$r[url])}
意思是,截取url,把http://www.替换成http://m.
到这里.就完成了手机版的配置了.在配套制作模板,就OK了!

附:
如果要在电脑版的网页上,加上当前页面手机版的链接
链接地址应该为:

http://{str_replace('www.','m.',$_SERVER['SERVER_NAME'])}{$_SERVER['REQUEST_URI']}

反之.手机版上,加上电脑版的链接

http://{str_replace('m.','www.',$_SERVER['SERVER_NAME'])}{$_SERVER['REQUEST_URI']}

当然,如果电脑版上.把网址链接改成二维码,那是最好的.
有了链接,.二维码还会难?

 

如果你是生成静态页面的.那么只要在页头加上以下JS代码.就可以搞定了

<script type="text/javascript">
function browserRedirect() {
var sUserAgent = navigator.userAgent.toLowerCase();
var bIsIpad = sUserAgent.match(/ipad/i) == "ipad";
var bIsIphoneOs = sUserAgent.match(/iphone os/i) == "iphone os";
var bIsMidp = sUserAgent.match(/midp/i) == "midp";
var bIsUc7 = sUserAgent.match(/rv:1.2.3.4/i) == "rv:1.2.3.4";
var bIsUc = sUserAgent.match(/ucweb/i) == "ucweb";
var bIsAndroid = sUserAgent.match(/android/i) == "android";
var bIsCE = sUserAgent.match(/windows ce/i) == "windows ce";
var bIsWM = sUserAgent.match(/windows mobile/i) == "windows mobile";
if (bIsIpad || bIsIphoneOs || bIsMidp || bIsUc7 || bIsUc || bIsAndroid || bIsCE || bIsWM) {
{if $catid=='' and $id==''}
window.location.href="{APP_PATH}/index.php";
{elseif $id=='' and $catid!=''}
window.location.href="{APP_PATH}/index.php?m=content&c=index&a=lists&catid={$catid}";
{else}
window.location.href="{APP_PATH}/index.php?m=content&c=index&a=show&catid={$catid}&id={$id}";
{/if}
}
}
browserRedirect();

function closewindow() {
$("#register-box").hide();
}
function openwindow() {
$("#register-box").show();
}
</script>

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多