萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> CMS 教學

帝國CMS實現上一篇/下一篇標題鏈接(支持字數截取、當前欄目、動態標簽多種方式)

方法一:官方動態鏈接(最簡單,無需修改模板)

直接在內容模板中使用內置動態地址:

<a href="[!--news.url--]e/public/GotoNext?classid=[!--classid--]&id=[!--id--]&enews=pre">上一篇</a>
<a href="[!--news.url--]e/public/GotoNext?classid=[!--classid--]&id=[!--id--]&enews=next">下一篇</a>

優點:零代碼,自動判斷當前欄目;缺點:無法控制標題長度,樣式固定。

方法二:靈動標簽 + SQL 查詢(靈活控制字數、樣式)

上一篇

[e:loop={"select id,classid,newspath,filename,groupid,titleurl from [!db.pre!]ecms_".$class_r[$navinfor[classid]]['tbname']." where id<".$navinfor[id]." and classid=".$navinfor[classid]." and checked=1 order by id desc limit 1",1,24,0}]
<?php
$titleurl=sys_ReturnBqTitleLink($bqr);
echo '<a href="'.$titleurl.'"><?=esub($bqr[title],30)?></a>';
?>
[/e:loop]

下一篇(將 < 改為 >,排序改為 ASC)

[e:loop={"select id,classid,newspath,filename,groupid,titleurl from [!db.pre!]ecms_".$class_r[$navinfor[classid]]['tbname']." where id>".$navinfor[id]." and classid=".$navinfor[classid]." and checked=1 order by id asc limit 1",1,24,0}]
<?php
$titleurl=sys_ReturnBqTitleLink($bqr);
echo '<a href="'.$titleurl.'"><?=esub($bqr[title],30)?></a>';
?>
[/e:loop]

關鍵點:

  • esub($bqr[title],30) 截取標題前 30 字符,防止過長破壞版面。
  • sys_ReturnBqTitleLink($bqr) 自動生成完整鏈接(支持偽靜態)。
  • 24 為操作類型碼(內容頁),保持不變。

方法三:靈動標簽簡潔寫法(推薦,代碼最少)

得益於帝國CMS 7.0+ 的 selfinfo 參數:

<!--上一篇-->
[e:loop={'selfinfo',1,0,0,'id<'.$navinfor[id].'','id desc'}]
<a href="<?=sys_ReturnBqTitleLink($bqr)?>">上一篇:<?=esub($bqr[title],30)?></a>
[/e:loop]

<!--下一篇-->
[e:loop={'selfinfo',1,0,0,'id>'.$navinfor[id].'','id asc'}]
<a href="<?=sys_ReturnBqTitleLink($bqr)?>">下一篇:<?=esub($bqr[title],30)?></a>
[/e:loop]

優點:無需手寫表名、字段,自動適配當前模型與欄目,代碼極簡。

進階技巧:無鄰近文章時的友好提示

<?php
$pre = $empire->fetch1("select id,title from [!db.pre!]ecms_{$class_r[$navinfor[classid]]['tbname']} where id<".$navinfor[id]." and classid=".$navinfor[classid]." and checked=1 order by id desc limit 1");
$next = $empire->fetch1("select id,title from [!db.pre!]ecms_{$class_r[$navinfor[classid]]['tbname']} where id>".$navinfor[id]." and classid=".$navinfor[classid]." and checked=1 order by id asc limit 1");
?>
<div class="prev-next">
  <span>上一篇:<?= $pre ? '<a href="'.sys_ReturnBqTitleLink($pre).'">'.esub($pre[title],30).'</a>' : '已經是第一篇' ?></span>
  <span>下一篇:<?= $next ? '<a href="'.sys_ReturnBqTitleLink($next).'">'.esub($next[title],30).'</a>' : '已經是最後一篇' ?></span>
</div>

總結建議

場景推薦方法理由
快速上線、無特殊樣式需求方法一(動態鏈接)零開發成本
需控制標題長度、自定義 HTML 結構方法三(selfinfo 靈動標簽)代碼最少、維護性最好
跨欄目、複雜篩選條件方法二(完整 SQL)完全可控查詢邏輯

根據項目實際需求選擇合適方案,即可輕鬆實現優雅的上下篇導航。

copyright © 萬盛學電腦網 all rights reserved