zoukankan      html  css  js  c++  java
  • phpcms实现手机端与PC端双模板的方法与详细教程(同步跳转)

    手机版比pc流量都要重要了,现在phpcms还没有开发手机版,下面我们就教大家如何让phpcms实现手机版访问,一般正常情况下我们电脑版的域名为http://www.你的域名/,而手机版的域名一般为http://m.你的域名/ 例如:   http://m.xxxxx.com

    找到并打开根目录 caches/configs/route.php

    添加

    return array(
    'default'=>array('m'=>'content', 'c'=>'index', 'a'=>'init'),
    'm.0317cn.net'=>array('m'=>'wap', 'c'=>'index', 'a'=>'init'),
    );

    这样就需要我们给手机端另外做一套模板,实现网站双模板。下面是具体方法:

    首先给phpcms添加自适配的代码,找到并打开/modules/content/index.php文件,在里面找到如下代码:


     

    1 include template('content',$template);
     

    将其修改为如下代码:


     

    if(substr($_SERVER['SERVER_NAME'], 0,1) == 'm'){
       include template('wap',$template);
       }else{
       include template('content',$template);
      }
     


    以上代码的意思是当前页面url中第一个字符为m时则调用wap模板,否则调用content模板

    但是由于phpcms把文章的url都固定写死在数据表中,所以页面中的标签不能在使用{$r[url]}

    而要改成{str_replace('http://www.','http://m.',$r[url])}

    意思是截取url,把http://www.你的域名/ 替换成http://m.你的域名/


    这里我们就完成了手机版的设置了,然后我们在制作一套手机端模板wap就可以了。

    如果我们要在PC端的内容里面加上当前页面手机端的链接,链接地址写法如下:

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


    反之,手机端加上PC端的链接:

    1 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="http://m.xxxxx.net/index.php";
    {elseif $id=='' and $catid!=''}
    window.location.href="http://m.xxxxx.net/index.php?m=content&c=index&a=lists&catid={$catid}";
    {else}
    window.location.href="http://m.xxxxxx.net/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>

    模板调用代码使用PC版的

    遇到问题--wap 版首页不能调用指定栏目名称,图片,连接

    解决办法:

    {pc:content action="category"  return="CATEGORYS" }

  • 相关阅读:
    SQL Server执行计划的理解
    SQL Server Profiler工具
    利用SQL Profiler处理开销较大的查询
    查询开销
    详解索引连接类型
    详解执行计划
    数据库引擎调整顾问
    six.moves的用法
    ※数据结构※→☆非线性结构(tree)☆============二叉树 顺序存储结构(tree binary sequence)(十九)
    C++ 线程的创建,挂起,唤醒,终止
  • 原文地址:https://www.cnblogs.com/lanne/p/13111996.html
Copyright © 2011-2022 走看看