discuz 帖子列表页调用帖子图片附件-Discuz教程下载

discuz 帖子列表页调用帖子图片附件

开通本站Svip会员,全站资源免费下
准确的说应该是调用多图,如果是一张图,直接设置封面即可,就不需要费这劲了。
网上也有方法,不过我不喜欢他们的方式。
代码可以直接写在模板forumdisplay_list.htm里,有不同数量不同样式
1.png
以默认模板为例:
打开模板forum/forumdisplay_list.htm,找到83行左右:
  1. <!--{loop $_G['forum_threadlist'] $key $thread}-->
复制代码
在其下增加
  1. <!--{eval $tbid = DB::result(DB::query("SELECT tableid FROM ".DB::table('forum_attachment')." WHERE `tid`= '$thread[tid]'"));}-->
  2. <!--{if $tbid}-->
  3. <!--{eval $picount = DB::fetch_all("SELECT aid FROM ".DB::table('forum_attachment_'.$tbid.'')." WHERE `tid`= '$thread[tid]' AND `isimage`=1;");}-->
  4. <!--{eval $picnum = count($picount);}-->
  5. <!--{if $picnum < 3}-->
  6. <!--{eval $litpicnum = '1';}-->
  7. <!--{elseif $picnum > 2 && $picnum < 6}-->
  8. <!--{eval $litpicnum = '3';}-->
  9. <!--{elseif $picnum > 5}-->
  10. <!--{eval $litpicnum = '6';}-->
  11. <!--{/if}-->
  12. <!--{eval $covers = DB::fetch_all("SELECT attachment,aid,description FROM ".DB::table('forum_attachment_'.$tbid.'')." WHERE `tid`= '$thread[tid]' AND `isimage`=1 LIMIT 0,$litpicnum;");}-->
  13. <!--{/if}-->
复制代码
再查找
  1. <!-- end of table "forum_G[fid]" branch 1/3 -->
复制代码
其上最近的</tr>之后增加
  1. <!--{if $tbid}-->
  2. <tr>
  3. <td></td>
  4. <td></td>
  5. <td colspan="4">
  6. <!--{loop $covers $thecover}-->
  7. <!--{if $litpicnum == 1}-->
  8. <a href="forum.php?mod=viewthread&tid=$thread[tid]" class="z pipe" title="$thecover[aid]"><img src="data/attachment/forum/$thecover['attachment']" width="100" height="100"/></a>
  9. <font class="z">This is what it is like to be with Trump.</font>
  10. <!--{else}-->
  11. <a href="forum.php?mod=viewthread&tid=$thread[tid]" class="z pipe" title="$thecover[aid]"><img src="data/attachment/forum/$thecover['attachment']" width="100" height="100"/></a>
  12. <!--{/if}-->
  13. <!--{/loop}-->
  14. </td>
  15. </tr>
  16. <!--{/if}-->
复制代码
再找到
  1. <!--{if !$thread['forumstick'] && ($thread['isgroup'] == 1 || $thread['fid'] != $_G['fid'])}-->
  2. <!--{if $thread['related_group'] == 0 && $thread['closed'] > 1}-->
  3. <!--{eval $thread[tid]=$thread[closed];}-->
  4. <!--{/if}-->
  5. <!--{if $groupnames[$thread[tid]]}-->
  6. <span class="fromg xg1"> [{lang from}: <a href="forum.php?mod=group&fid={$groupnames[$thread[tid]][fid]}" target="_blank" class="xg1">{$groupnames[$thread[tid]][name]}</a>]</span>
  7. <!--{/if}-->
  8. <!--{/if}-->
复制代码
其下增加
  1. <!--{if $tbid && !$picnum == 0}--><span style="color:red;"><!--{$picnum}-->P</span><!--{/if}--></a>
复制代码
结束。
具体的图片显示多少这里↓,<3显示1,>2且<6显示3,>5显示6。
  1. <!--{if $picnum < 3}-->
  2. <!--{eval $litpicnum = '1';}-->
  3. <!--{elseif $picnum > 2 && $picnum < 6}-->
  4. <!--{eval $litpicnum = '3';}-->
  5. <!--{elseif $picnum > 5}-->
  6. <!--{eval $litpicnum = '6';}-->
  7. <!--{/if}-->
复制代码
这个是匆忙写的,没注意有没有逻辑漏洞,如有错请提出。

后面具体的样式是这段↓
  1. <!--{if $litpicnum == 1}-->
  2. <a href="forum.php?mod=viewthread&tid=$thread[tid]" class="z pipe" title="$thecover[aid]"><img src="data/attachment/forum/$thecover['attachment']" width="100" height="100"/></a>
  3. <font class="z">This is what it is like to be with Trump.</font>
  4. <!--{else}-->
  5. <a href="forum.php?mod=viewthread&tid=$thread[tid]" class="z pipe" title="$thecover[aid]"><img src="data/attachment/forum/$thecover['attachment']" width="100" height="100"/></a>
  6. <!--{/if}-->
复制代码
如果图片数量等于1,后面带个摘要。
否则图片横排铺开。具体数量会按上面的显示。

通常情况下,两种样式即可,1图和多图,即1图显示在左右,多图在下面铺开。
如果多种样式,<!--{if $litpicnum == 数量}-->此数量时的表现形式</if>
当然,这的数量$litpicnum是受上面的代码中与$picnum关系的限定。
$picnum是实际总数量,$litpicnum是经过判断后允许显示的数量。

要下班了,写的差不多啦,stop here!

才知道一个鬼问题,不需要查询附件索引表,附件分表id就是帖子id最后一位。

如果“无图帖子”调用图片出了点问题,本来是无图帖子,却调用了临近上1条帖子的全部图片……
试试给<tr></tr>部分外层加上<!--{if $covers}--><!--{/if}-->,代码如下
  1. <!--{if $covers}-->
  2. <tr>
  3.     <td></td>
  4.     <td></td>
  5.     <td>
  6.     <!--{loop $covers $thecover}-->
  7.     <!--{if $litpicnum == 1}-->
  8.     <a href="forum.php?mod=viewthread&tid=$thread[tid]" class="z pipe" title="$thecover[aid]"><img src="data/attachment/forum/$thecover['attachment']" width="100" height="100"/></a>
  9.     <font class="z">This is what it is like to be with Trump.</font>
  10.     <!--{else}-->
  11.     <a href="forum.php?mod=viewthread&tid=$thread[tid]" class="z pipe" title="$thecover[aid]"><img src="data/attachment/forum/$thecover['attachment']" width="100" height="100"/></a>
  12.     <!--{/if}-->
  13.     <!--{/loop}-->
  14.     </td>
  15.     </tr>
  16. <!--{/if}-->
复制代码
意思是如果这个帖子没图片附件,这个<tr>就不让它显示。


如果正常情况下,咱们这个代码是没问题的,包括0图、1图、10图等都正常,但是在某个帖子有“新人贴、置顶、精华、热门”等TAG.html" target="_blank" class="relatedlink">标签的时候,就出现“图片错调”的问题。再具体点的问题描述比如,我注册了个新账号,发了个“0图新人贴”,系统自动打上了“新人贴”标签,结果它就自动调取了附近一条“1图”帖子的图,再比如,我把一条“0图”帖子全局置顶,然后之后发布的所有0图帖子,都开始调用附近的帖子的图片。我猜测,咱们是不是要加个什么判断,去除“新人贴、置顶、精华、热门”等标签的影响?

用if $covers判断不行,就换成if $tbid
图片数量显示代码换成:
  1. <!--{if $tbid && !$picnum == 0}--><span style="color:red;"><!--{$picnum}-->P</span><!--{/if}--></a>
复制代码
图片loop部分换成:
  1. <!--{if $tbid}-->
  2. <tr>
  3. <td></td>
  4. <td></td>
  5. <td>
  6. <!--{loop $covers $thecover}-->
  7. <!--{if $litpicnum == 1}-->
  8. <a href="forum.php?mod=viewthread&tid=$thread[tid]" class="z pipe" title="$thecover[aid]"><img src="data/attachment/forum/$thecover['attachment']" width="100" height="100"/></a>
  9. <font class="z">This is what it is like to be with Trump.</font>
  10. <!--{else}-->
  11. <a href="forum.php?mod=viewthread&tid=$thread[tid]" class="z pipe" title="$thecover[aid]"><img src="data/attachment/forum/$thecover['attachment']" width="100" height="100"/></a>
  12. <!--{/if}-->
  13. <!--{/loop}-->
  14. </td>
  15. </tr>
  16. <!--{/if}-->
复制代码
6.jpg 如果需要之查询一次,则将代码换成
  1. <!--{eval $tbid=substr($thread['tid'], -1);}-->
复制代码
获取到附件分表id,获取代码是
  1. <!--{eval $tbid = DB::result(DB::query("SELECT tableid FROM ".DB::table('forum_attachment')." WHERE `tid`= '$_G[tid]'"));}-->
复制代码
或者更简单一点
  1. <!--{eval $tbid=substr($_G['tid'], -1);}-->
复制代码

全部评论 0

您需要登录后才可以回帖 立即登录
登录
0
0
0
返回顶部