本教程針對 DedeCMS 問答模組(非 CMS 首頁)的首頁模板 index.htm,實現問題列表顯示標題、摘要與連結。
在欲顯示位置插入以下代碼:
<div class="rd">
<?php
if($digests != ""){
foreach($digests as $key => $v){
if($key == 0){
?>
<h1><a href="<?php echo $v['qurl'];?>" title="<?php echo cn_substr($v['title'],40);?>"><?php echo cn_substr($v['title'],38);?></a></h1>
<p><?php echo cn_substr(html2text($v['content']),120);?>……<a href="<?php echo $v['qurl'];?>">[詳細]</a></p>
<ul>
<?php }else{ ?>
<li><a href="<?php echo $v['qurl'];?>" title="<?php echo cn_substr($v['title'],40);?>"><?php echo cn_substr($v['title'],38);?></a><?php echo $v['userid'];?></li>
<?php }}} ?>
</ul>
</div>
關鍵函數: cn_substr() 截取中文字元,html2text() 過濾 HTML 標籤。
開啟 ask/model/mquestion.php,找到 get_digests() 函數,修改 SQL 查詢以包含 content 欄位:
function get_digests($row = '10')
{
$arrays = array();
$query = "SELECT a.id, a.content, a.title, m.userid FROM `dede_ask` a
LEFT JOIN `dede_member` m ON m.mid=a.uid
WHERE a.digest = 1 ORDER BY dateline DESC LIMIT 0,$row";
$this->dsql->SetQuery($query);
$this->dsql->Execute();
while($arr = $this->dsql->GetArray()){
$arrays[] = $arr;
}
return $arrays;
}
修改完成後,更新問答首頁緩存即可看到效果。