phpcms實現移動端和電腦端不同模板
1、首先打開phpcms/libs/functions/global.func.php,在文件最后面加一個isMobile()方法,用來判斷是否是手機端打開
?function?isMobile()?{ ??//?如果有HTTP_X_WAP_PROFILE則一定是移動設備 ??if?(isset($_SERVER['HTTP_X_WAP_PROFILE']))?{ ????return?true; ??} ??//?如果via信息含有wap則一定是移動設備,部分服務商會屏蔽該信息 ??if?(isset($_SERVER['HTTP_VIA']))?{ ????//?找不到為flase,否則為true ????return?stristr($_SERVER['HTTP_VIA'],?"wap")???true?:?false; ??} ??//?腦殘法,判斷手機發送的客戶端標志,兼容性有待提高。其中'MicroMessenger'是電腦微信 ??if?(isset($_SERVER['HTTP_USER_AGENT']))?{ ????$clientkeywords?=?array('nokia','sony','ericsson','mot','samsung','htc','sgh','lg','sharp','sie-','philips','panasonic','alcatel','lenovo','iphone','ipod','blackberry','meizu','android','netfront','symbian','ucweb','windowsce','palm','operamini','operamobi','openwave','nexusone','cldc','midp','wap','mobile','MicroMessenger'); ????//?從HTTP_USER_AGENT中查找手機瀏覽器的關鍵字 ????if?(preg_match("/("?.?implode('|',?$clientkeywords)?.?")/i",?strtolower($_SERVER['HTTP_USER_AGENT'])))?{ ??????return?true; ????} ??} ??//?協議法,因為有可能不準確,放到最后判斷 ??if?(isset?($_SERVER['HTTP_ACCEPT']))?{ ????//?如果只支持wml并且不支持html那一定是移動設備 ????//?如果支持wml和html但是wml在html之前則是移動設備 ????if?((strpos($_SERVER['HTTP_ACCEPT'],?'vnd.wap.wml')?!==?false)?&&?(strpos($_SERVER['HTTP_ACCEPT'],?'text/html')?===?false?||?(strpos($_SERVER['HTTP_ACCEPT'],?'vnd.wap.wml')?<p>2、然后打開phpcms/modules/content/index.php,有三個地方要改的</p><p>a)找到首頁的init方法,在最后加載模板的時候,做一個判斷,如果是手機端打開就加載手機端模板,如果是電腦端打開就加載電腦端模板</p><p>大概在31行找到:</p><p><span>立即學習</span>“<a href="https://pan.quark.cn/s/7fc7563c4182" style="text-decoration: underline !important; color: blue; font-weight: bolder;" rel="nofollow" target="_blank">PHP免費學習筆記(深入)</a>”;</p><pre class="brush:js;toolbar:false">include?template('content','index',$default_style);
改成:
if(isMobile()){ ??????include?template('mobile','index',$default_style); }else{ ?????include?template('content','index',$default_style); }
b)找到內容頁的show方法,同樣在最后加載模板的時候做一個判斷
大概在203行找到:
include?template('content',$template);
改成:
if(isMobile()){ ?????include?template('mobile',$template); }else{ ?????include?template('content',$template); }
c)找到列表頁的lists方法,同樣在最后加載模板的時候做一個判斷
大概在265行和278行,這里有兩處,找到:
include?template('content',$template); 改成: if(isMobile()){ ?????include?template('mobile',$template);}else{????? ?????include?template('content',$template); }
在你當前的模板目錄下新建一個mobile目錄,用來存放手機端模板
如果你當前的模板目錄是phpcms/templates/default,那么你就在phpcms/templates/default下面建一個mobile目錄。
如果你當前的模板目錄是phpcms/templates/moban,那么你就在phpcms/templates/moban下面建一個mobile目錄。
這樣就可以實現電腦端和手機端分別加載不同的模板。
PHP中文網,大量的免費PHPCMS教程,歡迎在線學習!
? 版權聲明
文章版權歸作者所有,未經允許請勿轉載。
THE END
喜歡就支持一下吧
相關推薦