最近给客户部署了一套织梦的网站源码,由于要求手机端和PC端同步,所以就对移动端进行了适配。但是由于织梦移动端的模板代码是生成在m文件夹下,而对移动端又绑定了相应的二级域名。所以造成了,网站在收录的时候,收录了m文件下的html页面,但是当在电脑端查看收录页面时,会发现有很多m路径下的收录页面,当访问的时候,打开的是移动网站的页面,这样对用户体验不好,所以需要做相应的跳转,以下的解决方法是建立在PC端是静态页面、而移动端是动态调用的基础之上,如果移动端页是静态页面,请自行查找解决办法。
移动端首页跳转至PC端首页的代码:
<script type="text/javascript"> /*判断手机端访问*/ var isMobile = { Android:function(){ return navigator.userAgent.match(/Android/i) ? true:false; }, BlackBerry:function(){ return navigator.userAgent.match(/BlackBerry/i) ? true:false; }, iOS:function(){ return navigator.userAgent.match(/iPhone|iPad|iPod/i) ? true:false; }, Windows:function(){ return navigator.userAgent.match(/IEMobile/i) ? true:false; }, any:function(){ return (isMobile.Android()||isMobile.BlackBerry() ||isMobile.iOS()||isMobile.Windows()); } }; if(!isMobile.any()) { window.location.href = "PC端网址"; } </script>
移动端文章页面跳转至PC端代码:
<script type="text/javascript"> /*判断手机端访问*/ var isMobile = { Android:function(){ return navigator.userAgent.match(/Android/i) ? true:false; }, BlackBerry:function(){ return navigator.userAgent.match(/BlackBerry/i) ? true:false; }, iOS:function(){ return navigator.userAgent.match(/iPhone|iPad|iPod/i) ? true:false; }, Windows:function(){ return navigator.userAgent.match(/IEMobile/i) ? true:false; }, any:function(){ return (isMobile.Android()||isMobile.BlackBerry() || isMobile.iOS()||isMobile.Windows()); } }; if(!isMobile.any()) { window.location.href = "{dede:field.id runphp='yes'} $id=@me;@me='';$url=GetOneArchive($id); @me=$url['arcurl'];{/dede:field.id}"; } </script>
移动端列表页面跳转至PC端代码:
把以下代码加到 /include/helpers/extend.helper.php 文件最后。
if ( ! function_exists('itemCurUrl')) { /** * 获取当前列表页或内容页的URL * 未经大量测试,一定会有不如意的地方,使用时注意测试观察 * * 列表页使用方法: * dede:field.id runphp='yes'}@me=itemCurUrl(@me,'list'); {/dede:field.id} * 内容页使用方法: * dede:field.id runphp='yes'}@me=itemCurUrl(@me,'view'); {/dede:field.id} * * @param int $id 列表ID或文章ID * @param string $type 分类,list:列表页,view:内容页 * @return string */ function itemCurUrl($id, $type='list') { global $dsql; if($type == 'list'){ $sql="SELECT * FROM `dede_arctype` WHERE `id`='$id' "; $info=$dsql->GetOne($sql); $url=GetOneTypeUrlA($info); return $url; }else{ $arr = GetOneArchive($id); if(!empty($arr['ismake'])){ // 静态页面 return $arr['arcurl']; }else{ // 动态页面 return '/plus/view.php?aid='.$arr['aid']; } } } }
然后在列表页的head部分加入以下代码:
<script type="text/javascript"> /*判断手机端访问*/ var isMobile = { Android: function () { return navigator.userAgent.match(/Android/i) ? true:false; }, BlackBerry:function(){ return navigator.userAgent.match(/BlackBerry/i) ? true:false; }, iOS:function(){ return navigator.userAgent.match(/iPhone|iPad|iPod/i) ? true:false; }, Windows:function(){ return navigator.userAgent.match(/IEMobile/i) ? true:false; }, any:function(){ return (isMobile.Android()||isMobile.BlackBerry() || isMobile.iOS()||isMobile.Windows()); } }; if (!isMobile.any()) { window.location.href = "{dede:field.id runphp='yes'} @me=itemCurUrl(@me,'list');{/dede:field.id}" } </script>
以上代码由本站亲自测试,可正常运行。