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" }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM