- 调用 PC 端的搜索程序(最简单、最常用)
- 为移动端创建独立的搜索功能(更灵活、功能更强)
准备工作:确保移动端模板已启用
在开始之前,请确保你已经正确配置了 DedeCMS 的移动端模板,你需要在后台设置一个移动端域名,并确保你的手机模板文件(如 index_m.htm, list_m.htm, article_m.htm 等)位于 /templets/你的手机模板文件夹/ 目录下。

调用 PC 端的搜索程序(最简单、最常用)
这种方法的核心思想是:移动端的搜索框直接指向 PC 端的搜索处理页面 plus/search.php,但通过参数控制只搜索移动端的内容,或者直接返回一个适合手机浏览的搜索结果页面。
步骤 1:在手机模板的搜索框中添加 mobile 参数
打开你的手机模板首页文件(通常是 index_m.htm),找到搜索框的 <form> 标签,修改它,增加一个 mobile 隐藏字段。
原始 PC 端搜索框代码示例:
<form name="formsearch" action="/plus/search.php">
<input type="hidden" name="kwtype" value="0" />
<input type="text" name="q" class="search-keyword" placeholder="请输入关键词" />
<button type="submit" class="search-submit">搜索</button>
</form>
修改后的手机端搜索框代码示例:
关键在于增加 <input type="hidden" name="mobile" value="1" />。

<form name="formsearch" action="/plus/search.php" method="get">
<input type="hidden" name="kwtype" value="0" />
<input type="hidden" name="mobile" value="1" /> <!-- 新增此行,用于标识移动端搜索 -->
<input type="text" name="q" class="search-keyword" placeholder="请输入关键词" />
<button type="submit" class="search-submit">搜索</button>
</form>
步骤 2:修改 PC 端的搜索处理文件 plus/search.php
我们需要修改 search.php 文件,让它能够识别 mobile 参数,并根据这个参数来决定返回哪种风格的搜索结果页。
- 找到
search.php文件:它位于你的网站根目录的/plus/文件夹下。 - 在文件开头添加判断逻辑:在
require_once(dirname(__FILE__)."/../include/config_base.php");这行代码之后,添加如下代码:
// 判断是否为移动端搜索
$isMobileSearch = isset($_GET['mobile']) && $_GET['mobile'] == '1';
// 如果是移动端搜索,则引入手机搜索结果模板
if ($isMobileSearch) {
$tempfile = str_replace("{style}", $cfg_df_style, $cfg_templets_dir."/search_list_m.htm");
} else {
// 否则,引入PC端搜索结果模板
$tempfile = str_replace("{style}", $cfg_df_style, $cfg_templets_dir."/search_list.htm");
}
解释:
isset($_GET['mobile']) && $_GET['mobile'] == '1':检查 URL 中是否存在mobile=1这个参数。$cfg_templets_dir."/search_list_m.htm":这里指定了移动端的搜索结果模板文件名。你需要确保这个模板文件存在,你可以直接复制/templets/你的手机模板文件夹/目录下的list_m.htm文件,并将其重命名为search_list_m.htm,然后根据需要进行修改(调整标题、摘要的显示样式)。
步骤 3:创建移动端搜索结果模板 search_list_m.htm
- 复制文件:将你的手机列表页模板
list_m.htm复制一份,命名为search_list_m.htm,并放到与list_m.htm相同的目录下(通常是/templets/你的手机模板文件夹/)。 - 修改模板内容:打开
search_list_m.htm,调整循环内的标签,使其更适合搜索结果的展示,可以突出显示关键词,或者调整摘要的长度。
search_list_m.htm 中的核心循环代码示例:
{dede:list pagesize='10'}
<li>
<h3><a href="[field:arcurl/]">[field:title function='htmlspecialchars(@me)'/]</a></h3>
<p class="summary">[field:info/]...</p>
<p class="info"><span class="time">[field:pubdate function="MyDate('Y-m-d',@me)"]</span></p>
</li>
{/dede:list}
步骤 4:创建分页模板(可选)
为了让搜索结果也能分页,你还需要创建一个移动端的分页模板。

- 复制 PC 端的分页模板
list_paginate.htm,命名为search_list_paginate_m.htm。 - 修改
search_list_paginate_m.htm的样式,使其适应手机屏幕。
修改 plus/search.php 文件以支持新的分页模板:
在刚才添加的代码后面,再添加一行:
// ... (接上面的代码)
if ($isMobileSearch) {
$tempfile = str_replace("{style}", $cfg_df_style, $cfg_templets_dir."/search_list_m.htm");
$tplfile = str_replace("{style}", $cfg_df_style, $cfg_templets_dir."/search_list_paginate_m.htm"); // 新增
} else {
$tempfile = str_replace("{style}", $cfg_df_style, $cfg_templets_dir."/search_list.htm");
$tplfile = str_replace("{style}", $cfg_df_style, $cfg_templets_dir."/list_paginate.htm"); // 默认PC分页
}
然后在文件中找到 $dtp->LoadTemplate($tempfile); 这一行,在其附近添加对分页模板的设置:
// ... (dtp->LoadTemplate 之前)
if ($isMobileSearch) {
$dtp->SetTemplate($tempfile);
// 加载移动端分页模板
$parseNow = TRUE;
$artlist = '';
$pageno = ($this->PageNo - 1) * $this->PageSize + 1;
$enddd = $this->TotalResult;
$this->Fields['pagelang'] = $lang_page;
$this->Fields('pageList', $this->GetPageListST($list_len, $listitem));
$this->Fields('pageNo', $pageno);
$this->Fields('end', $enddd);
$this->Fields('totalResult', $this->TotalResult);
$this->Fields('pageNo', $this->PageNo);
$this->Fields('totalPage', $this->TotalPage);
$this->Fields('pageSize', $this->PageSize);
$this->Fields('indexUrl', $this->GetCurUrl());
$dtp2 = new DedeTemplate();
$dtp2->LoadTemplate($tplfile);
$artlist = $dtp2->GetVar('artlist');
$dtp->Assign('pagelist', $artlist);
}
// ... (原来的逻辑)
$dtp->LoadTemplate($tempfile);
注意:上面的分页代码逻辑较为复杂,一个更简单的办法是直接修改 list_m.htm 中的分页标签 [pagebreak/],让它指向移动端的搜索结果页。
为移动端创建独立的搜索功能(更灵活)
这种方法更彻底,它为移动端创建了一套完全独立的搜索流程,包括处理页面和结果模板,这需要你创建新的文件。
步骤 1:创建移动端搜索处理文件 m/search.php
- 在你的网站根目录下创建一个名为
m的文件夹(如果还没有的话)。 - 在
/m/文件夹中,复制/plus/search.php文件,并将其命名为search.php。 - 修改
/m/search.php文件:- 找到
$tempfile = ...这一行。 - 将其修改为直接指向你的手机搜索结果模板路径,
$tempfile = str_replace("{style}", '你的手机模板文件夹名', $cfg_templets_dir."/search_list_m.htm"); - 同样,如果你需要自定义分页,也修改分页模板的路径。
- 找到
步骤 2:在手机模板中指向新的搜索处理文件
修改你的手机模板 index_m.htm 中的搜索框 action 属性,让它指向 /m/search.php。
<form name="formsearch" action="/m/search.php" method="get"> <!-- 修改这里的路径 -->
<input type="hidden" name="kwtype" value="0" />
<input type="text" name="q" class="search-keyword" placeholder="请输入关键词" />
<button type="submit" class="search-submit">搜索</button>
</form>
步骤 3:创建移动端搜索结果模板 search_list_m.htm
这一步与方法一中的步骤 3 完全相同,你需要确保 /templets/你的手机模板文件夹/ 目录下有 search_list_m.htm 这个模板文件。
优点与缺点
- 优点:
- 完全独立:移动端和 PC 端的搜索逻辑和模板完全分离,互不干扰。
- 性能可能更好:可以针对移动端做专门的优化,比如减少不必要的数据库查询。
- 更灵活:可以给移动端搜索添加额外的功能,比如按分类筛选、热门搜索推荐等,而不会影响 PC 端。
- 缺点:
- 需要创建新文件:比方法一多了一个复制和修改文件的操作。
- 维护稍复杂:如果未来 DedeCMS 更新了
search.php,你的/m/search.php不会自动更新,需要手动同步。
总结与推荐
| 特性 | 方法一(调用PC端) | 方法二(独立移动端) |
|---|---|---|
| 实现难度 | 简单 | 中等 |
| 灵活性 | 一般 | 高 |
| 维护成本 | 低(修改一个文件) | 较高(维护多个文件) |
| 适用场景 | 追求快速上线,移动端和PC端搜索功能差异不大。 | 对移动端体验要求高,需要定制化搜索功能。 |
对于大多数用户,我强烈推荐使用【方法一】,它最简单,能快速解决问题,并且通过修改 search.php 已经能满足大部分移动端搜索的需求。
只有在你的项目对移动端有非常高的定制化要求,或者你希望移动端和PC端的搜索功能在未来有完全不同的发展路径时,才考虑使用【方法二】。
希望这个详细的教程能帮助你成功在 DedeCMS 手机模板中实现搜索功能!
