15621857753

phpcmsV9怎么让同个链接在PC和手机浏览不同内容

来源:齐鲁建站 栏目:开发教程 阅读: 日期:2021-05-16

本文介绍了phpcmsV9怎么让同个链接在PC和手机浏览不同内容,PHPCMS可以通过同链接的手机和PC浏览不同的页面,实际上只需在PC站添加判断手机站浏览的页面连接即可。

废话就不多说了 直接进入到正题吧。

1、首先我们要添加的就是自适配的代码,找到/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中出现的第一个字符出现了一个m的时候,那么会自动调用到手机的模板,那么这里的m您可以修改为其他的,这个随便您的,但是这里需要注意的一个问题就是,phpcms的链接都是写入到数据库中的所以我们 在调用标签的时候不要再次使用{$r[url]},而是需要这样的改动{str_replace('http://www.','http://m.',$r[url])},就这样就可以搞定了手机版本与电脑版本的设置了

2、这个时候我们需要加入一个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>

这段代码您只需要添加到模板页头即可实现了。 希望这篇文章能帮助到站长朋友们!

展开