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’] = ‘/uploads/2024/’.$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’] = ‘/uploads/2024/’.’5izixue.jpg’;
}
else{
$post[‘relateitem’][$k][‘img’] = ‘/uploads/2024/’.$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相关帖子主题封面获取的方法

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论

微信小程序

微信扫一扫体验

立即
投稿

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部