Discuz获取相关帖子封面的方法教程

overme7个月前 (10-17)网页设计347
Discuz系统帖子内容页是带有相关帖子推荐功能的,根据帖子的Tags进行相关推荐。小编在进行手机端模板开发时,使用了PC端功能,显示效果如下图所示,只有帖子标题,没有封面。

为提高访客体验,在相关帖子下方帖子左侧增加主题封面显示。

步骤一:打开source/module/forum/forum_viewthread.php文件
搜索$post[‘tags’],修改为如下内容

  1. if($post[‘tags’]) {
  2.                                 $post[‘relateitem’] = getrelateitem($post[‘tags’], $post[‘tid’], $_G[‘setting’][‘relatenum’], $_G[‘setting’][‘relatetime’]);
  3.                                 foreach($post[‘relateitem’] as $k=>$img)
  4.                                    {
  5.                                         $threada= C::t(‘forum_attachment’)->fetch_all_by_id(‘tid’, $img[‘tid’], ‘aid’);
  6.                                                 $threadaid = reset($threada);
  7.                                                 $threadpic = C::t(‘forum_attachment_n’)->fetch_by_aid_uid($threadaid[‘tableid’], $threadaid[‘aid’], $thread[‘authorid’]);
  8.                                                 $thread[‘pic’] = $threadpic[‘attachment’];
  9.                                                 $post[‘relateitem’][$k][‘img’] = ‘data/attachment/forum/’.$thread[‘pic’];
  10.                                    }
  11.                         }

复制代码

参考上方代码后,相关帖子有封面输出了,但是部分没有封面的的主题,加载失败显示“点击重新加载”。如何处理参考下方内容:

[contentrestriction]if($post[‘tags’]) {
$post[‘relateitem’] = getrelateitem($post[‘tags’], $post[‘tid’], $_G[‘setting’][‘relatenum’], $_G[‘setting’][‘relatetime’]);
foreach($post[‘relateitem’] as $k=>$img)
{
$threada= C::t(‘forum_attachment’)->fetch_all_by_id(‘tid’, $img[‘tid’], ‘aid’);
$threadaid = reset($threada);
$threadpic = C::t(‘forum_attachment_n’)->fetch_by_aid_uid($threadaid[‘tableid’], $threadaid[‘aid’], $thread[‘authorid’]);
$thread[‘pic’] = $threadpic[‘attachment’];
if(empty($thread[‘pic’])){
$post[‘relateitem’][$k][‘img’] = ‘data/attachment/forum/’.’5izixue.jpg’;
}
else{
$post[‘relateitem’][$k][‘img’] = ‘data/attachment/forum/’.$thread[‘pic’];
}
}
}
为什么添加了if(empty($thread[‘pic’]))的判断?
如果遇到主题确实没有封面,如果不添加该判断,则图片资源加载出错,网页布局会变化,用户体验不好。添加判断后,主题没有封面时,可以替换为固定的封面。[/contentrestriction]

步骤二:
在帖子模板中,添加如下内容:

  1. <div class=”m_relation”>
  2.                                     <h3>{lang related_thread}</h3>
  3.                                     <ul>
  4.                                             <!–{loop $post[‘relateitem’] $var}–>
  5.                                             <li>
  6.                                             <a href=”forum.php?mod=viewthread&tid=$var[tid]” title=”$var[subject]” class=”topic_img”><img src=”$var[img]” width=”100px” height=”60px”></a>
  7.                                             <span class=”related_des”>
  8.                                                  <div class=”related_title”><a href=””>$var[subject]</a></div>
  9.                                                  <span class=”related_views”><i class=”icon-eye1″></i></span>
  10.                                             </span>
  11.                                             </li>
  12.                                             <!–{/loop}–>
  13.                                     </ul>
  14.                             </div>

其中$var[img]就是获取的相关帖子的封面地址

优化后的显示效果如下图:

以上,就是Discuz相关帖子主题封面获取的方法

相关文章


		Discuz相关帖子功能开启方法

Discuz相关帖子功能开启方法

Discuz系统相同帖子功能,是非常不错的功能,可以根据标签,匹配相关内容供用户浏览. 如下图示: 在此分享相关帖子功能开启方法: 步骤1: 进入后台 -> 界面 ->界面设置-&...

Discuz建站系统default目录结构说明

/template/default/common/  公共模板目录全局加载 /template/default/common/block_forumtree.htm  DIY论坛树形列表模块 /t...


		Discuz去除图片下载附件提示信息

Discuz去除图片下载附件提示信息

使用Discuz建站系统发布帖子内容时,内容有图片时。鼠标移动到图片上时,会弹出下图所示的“下载附件”的提示信息。 实际大家浏览网页时,基本上是不会主动下载图片的,只是查看而已。但是,下载附件的提...


		Discuz图片水印设置方法教程

Discuz图片水印设置方法教程

发布图片时,为了防止侵权盗用,或是为了宣传网址引流,需要添加在图片上添加水印。 在此分享Discuz系统图片水印设置方法: Discuz论坛的水印设置很简单,使用制作的水印图片覆盖系统水印地址,然...

discuz通过{eval }在模板中运行 PHP语句

为了安全起见,discuz 系统默认模板文件无法直接用 <?php … ?> 来运行PHP语句,只能通过固定标签 {eval} 在模板中运行 PHP 语句。 discuz模板php单行...

Discuz论坛入口文件forum.php解析

//定义两个常量,定义当前应用ID define(‘APPTYPEID’, 2); //定义当前应用脚本名称 define(‘CURSCRIPT’, ‘forum’); //引入两个核心库文件 r...

发表评论

访客

看不清,换一张

◎欢迎参与讨论,请在这里发表您的看法和观点。