请选择 进入手机版 | 继续访问电脑版

硬汉工作室

搜索
热搜: 活动 交友 discuz

[开发技术] Discuz分类信息模板里调用变量

[复制链接]
发表于 2019-2-20 16:12:11 | 显示全部楼层 |阅读模式
帖子页可以调用分类信息的变量,但是也仅仅只能调用定义好的分类信息变量,如果我们想调用其它变量,比如帖子缩略图,那该如何做呢?这得修改源码了。

打开forum_viewthread.php文件,搜索$threadsortshow = threadsortshow($thread['sortid'], $_G['tid']);这行,这是个数组,其中$threadsortshow['typetemplate']是根据你帖子页分类信息的模板生成的html代码,有分类信息变量的会替换成实际的值。

比如我们在分类信息模板里定义了一个{thumb}标示,这代表的是帖子缩略图,但是因为这个不是分类信息变量,所以生成的html代码里还是显示{thumb},我们可以把这个标识替换成实际的变量,代码如下:
if(strpos($threadsortshow['typetemplate'], '{thumb}') !== FALSE) { // 先判断下生成的html里是否有这个标识
    $thumb = getthreadcover($_G['tid'], 1); // 获取帖子封面代码
    $threadsortshow['typetemplate'] = str_replace('{thumb}', $thumb, $threadsortshow['typetemplate']); // 替换{thumb}为实际的封面地址
}

帖子页分类信息变量插件已经上线,无需再修改代码啦。访问地址:http://addon.discuz.com/?@nciaer_sortvar.plugin


帖子列表里的分类信息页可以加变量,就是比较复杂了。

----华丽的分割线----

下面讲的是列表页分类信息模板里调用变量。
打开function_threadsort.php文件,这个是分类信息处理的函数文件。找到以下代码:
foreach($threadlist as $thread) {
        foreach($thread as $k => $v) {
                $replaces['{'.$k.'}'] = $v;
        }
        $body = $sortlistarray['template'][$sortid][$thread['tid']];
        $replaces['{author_url}'] = $rewritespace ? rewriteoutput('home_space', 1, '', $thread['authorid']) : 'home.php?mod=space&uid='.$thread['authorid'];
        $replaces['{lastposter_url}'] = $rewritespace ? rewriteoutput('home_space', 1, '', '', $thread['lastposter']) : 'home.php?mod=space&username='.$thread['lastposterenc'];
        $replaces['{subject_url}'] = $rewriteviewthread ? rewriteoutput('forum_viewthread', 1, '', $thread['tid']) : 'forum.php?mod=viewthread&tid='.$thread['tid'];
        $replaces['{lastpost_url}'] = 'forum.php?mod=redirect&tid='.$thread['tid'].'&goto=lastpost#lastpost';
        $replaces['{lastpost_url}'] = 'forum.php?mod=redirect&tid='.$thread['tid'].'&goto=lastpost#lastpost';
        $replaces['{avatar_small}'] = avatar($thread['authorid'], 'small', true);
        $replaces['{typename_url}'] = 'forum.php?mod=forumdisplay&fid='.$fid.'&filter=typeid&typeid='.$thread['tid'];
        $replaces['{attachment}'] = ($thread['attachment'] == 2 ? '<img src="'.STATICURL.'image/filetype/image_s.gif" align="absmiddle" />' :
                                        ($thread['attachment'] == 1 ? '<img src="'.STATICURL.'image/filetype/common.gif" align="absmiddle" />' : ''));
        $replaces['{author_verify}'] = $verify[$thread['authorid']] ? $verify[$thread['authorid']] : '';
        if($_G['forum']['ismoderator']) {
                if($thread['fid'] == $fid && $thread['displayorder'] <= 3 || $_G['adminid'] == 1) {
                        $replaces['{modcheck}'] = '<input type="checkbox" name="moderate[]" value="'.$thread['tid'].'" />';
                } else {
                        $replaces['{modcheck}'] = '<input type="checkbox" disabled="disabled" />';
                }
        } else {
                $replaces['{modcheck}'] = '';
        }
        $body = str_replace(array_keys($replaces), $replaces, $body);
        $sorttemplate['body'] .= $body;
}

这些代码就是处理系统的分类信息变量替换的,比如把{subject_url}替换成帖子地址,我们如果想要增加新的变量,就根据其格式在$replaces数组里添加新的替换变量,如
$replaces['{tid}'] = $thread['tid']; // 这个是增加帖子tid的分类信息模板变量。
有关PHP系统、Discuz或网站等各种问题,可以联系QQ1069971363寻求付费支持
Discuz插件商店:http://addon.dismall.com/?@56030.developer
回复

使用道具 举报

QQ|Archiver|手机版|小黑屋|硬汉工作室 ( 冀ICP备13021567号-9 )

GMT+8, 2024-4-16 16:43 , Processed in 0.056465 second(s), 22 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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