PHPCMS V9 內建模板系統,透過自訂函式偵測行動裝置、載入獨立手機版模板目錄、調整連結為動態模式,即可低成本建立 WAP 站點。本文提供完整實作步驟、程式碼與避坑指南。
編輯 phpcms/libs/functions/extention.func.php:
<?php
/**
* 判斷是否為行動裝置存取
* @return bool
*/
function is_mobile() {
// 1. Proxy/透明代理特徵
if (isset($_SERVER['HTTP_VIA']) ||
isset($_SERVER['HTTP_X_NOKIA_CONNECTION_MODE']) ||
isset($_SERVER['HTTP_X_UP_CALLING_LINE_ID'])) {
return true;
}
// 2. Accept 標頭包含 WML
if (stripos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') !== false) {
return true;
}
// 3. User-Agent 關鍵字比對
$ua = isset($_SERVER['HTTP_USER_AGENT']) ? strtolower($_SERVER['HTTP_USER_AGENT']) : '';
if (empty($ua)) return true; // 無 UA 視為行動裝置
// 排除平板 (iPad 等)
if (stripos($ua, 'ipad') !== false) return false;
$mobileKeywords = [
'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',
'opera mobi', 'openwave', 'nexusone', 'cldc', 'midp', 'wap', 'mobile'
];
foreach ($mobileKeywords as $keyword) {
if (stripos($ua, $keyword) !== false) return true;
}
return false;
}
phpcms/templates/
├── default/ # PC 版模板 (原有)
│ ├── content/
│ ├── index.php
│ └── ...
└── mobile/ # 新增:手機版模板目錄
├── content/
│ ├── index.html # 列表頁
│ ├── show.html # 內容頁
│ └── category.html # 欄目頁
├── index.php # 入口 (可選)
└── header.html / footer.html
命名規則:手機版模板檔名 必須與 PC 版完全一致,系統才能自動對應切換。
編輯 phpcms/modules/content/index.php (或其他模組控制器):
<?php
// 原始碼區塊:載入模板
// 修改為條件載入
$template = 'default_template_name'; // 原有邏輯取得的模板名
if (is_mobile()) {
// 載入 mobile 目錄下的同名模板
include template('mobile', $template);
} else {
// 載入預設 default 目錄
include template('content', $template);
}
需分別修改三個動作方法:
index() - 列表頁 → template('mobile', 'list')category() - 欄目頁 → template('mobile', 'category')show() - 內容頁 → template('mobile', 'show')手機版建議不生成靜態頁,改用動態連結避免快取問題:
<!-- 列表頁連結 -->
<a href="index.php?m=content&c=index&a=show&catid={$catid}&id={$id}">{$title}</a>
<!-- 分頁連結 -->
<a href="index.php?m=content&c=index&a=lists&catid={$catid}&page={$page}">下一頁</a>
<!DOCTYPE html>
<html><head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<meta name="format-detection" content="telephone=no">
<title>{$title} - {$SEO['site_title']}</title>
<link rel="stylesheet" href="{$CSS_PATH}mobile.css">
</head><body>
<header><h1>{$title}</h1></header>
<article>
<time>{$inputtime}</time>
{$content}
</article>
<footer>
<a href="index.php?m=content&c=index&a=lists&catid={$catid}">返回列表</a>
</footer>
</body></html>
* { box-sizing:border-box; -webkit-tap-highlight-color:transparent; }
body { font-size:16px; line-height:1.6; margin:0; padding:10px; word-wrap:break-word; }
img, video { max-width:100%; height:auto; display:block; }
a { -webkit-tap-highlight-color:rgba(0,0,0,0); text-decoration:none; color:#0066cc; }
header, footer, article, section { display:block; }
| 問題 | 原因 | 解決 |
|---|---|---|
| 手機版仍顯示 PC 模板 | is_mobile() 判斷失效 / 模板目錄名稱錯誤 | 檢查 UA 關鍵字、確認 mobile 目錄存在且檔名相同 |
| 靜態頁面不切換 | 後台已生成靜態 HTML | 手機版關閉 HTML 生成,改用動態連結 |
| 圖片/樣式路徑錯誤 | 模板中使用相對路徑 | 使用 {$CSS_PATH}、{$JS_PATH}、{$IMG_PATH} 標籤 |
| 分頁/列表連結指向 PC 版 | 模板未改動態連結 | 全站檢查連結改為 index.php?m=... 格式 |
| 平板裝置誤判為手機 | UA 包含 mobile 關鍵字 | 優先排除 ipad/android tablet 等平板特徵 |
m.example.com 指向同一代碼,Nginx 判斷 $host 設定變數,模板邏輯讀取變數而非 is_mobile(),利於 SEO 與快取。