DZ原生代码交流学习,买插件不如自己动手解决
 
发新帖
楼主: 天启
查看: 725|回复: 0

[教程] DZ论坛/群组帖子列表页调用摘要的两种方法

[复制链接]
天启VIP6 发表于 2019-10-29 17:50:55 | 显示全部楼层
本帖最后由 天启 于 2019-10-29 17:52 编辑

以下两种方法通用于论坛和群组。

方法 一:
打开/source/module/forum/目录下的forum_forumdisplay.php文件,
查找:$threadids[$threadindex] = $thread['tid'];(812行左右)
在其下方增加以下代码:
  1. //内容摘要
  2.          include_once libfile('function/post');
  3.         include_once libfile('function/attachment');
  4.         $thread['post'] = C::t('forum_post')->fetch_all_by_tid_position($thread['posttableid'],$thread['tid'],1);
  5.         $thread['post'] = array_shift($thread['post']);
  6.         $thread['preview'] = messagecutstr($thread['post']['message'], 200);//摘要字符数
  7.         $attachments = C::t('forum_attachment_n')->fetch_all_by_id('tid:'.$thread['post']['tid'], 'pid', $thread['post']['pid']);
  8.         $attachs = $imgattachs = array();
  9.         foreach(C::t('forum_attachment')->fetch_all_by_id('pid', $thread['post']['pid'], 'aid') as $attach) {
  10.                 $attach = array_merge($attach, $attachments[$attach['aid']]);
  11.                 $attach['filenametitle'] = $attach['filename'];
  12.                 $attach['ext'] = fileext($attach['filename']);
  13.                 getattach_row($attach, $attachs, $imgattachs);
  14.         }
  15.         $thread['attachments'] = $imgattachs;
  16.         //内容摘要END
复制代码
模版中就可以用$thread['preview']变量调用摘要了;
$thread['preview'] = messagecutstr($thread['post']['message'], 200);中的数字200是摘要字符数限制,可根据需要修改。


方法二:
以下两组代码均可直接调用帖子内容摘要,此方法是直接在模版中执行查询,故不推荐此法。
代码1:
  1. <!--{eval require_once(Discuz_ROOT."./source/function/function_post.php");}--><!--{echo messagecutstr(DB::result_first('SELECT `message` FROM '.DB::table('forum_post').' WHERE `tid` ='.$thread[tid].' AND `first` =1'),300);}-->
复制代码

代码2:
  1. <!--{eval $threadlist_message = DB::result(DB::query("SELECT message FROM ".DB::table('forum_post')." WHERE `tid` = $thread[tid]  AND `first` =1"));}-->
  2. <!--{echo cutstr($threadlist_message,300)}-->
复制代码


快速回复 返回顶部 返回列表