首页 > 好文分享 > 技术教程内容详情
广州信息网
久久优惠网

Discuz!教程之 diy增加主题调用帖子随机排序功能

2022-04-17 20:52:24 技术教程

简单的说,就是每隔一段时间(自己设定的数据缓存时间),即使没有新帖子,这个diy模块所调用的帖子也会变。

方式:加个选项,然后查询数据的时候把这个选项作为一个条件,选它就按它来调用。

 

具体操作:
1.打开/source/class/block/forum/block_threadhot.php,找到代码
array('recommends', 'threadlist_orderby_recommends'),
之下增加
array('rand', '随机'),

2.打开/source/class/block/forum/block_thread.php,找到代码

'orderby' => array(
    'title' => 'threadlist_orderby',
    'type'=> 'mradio',
    'value' => array(
        array('lastpost', 'threadlist_orderby_lastpost'),
        array('dateline', 'threadlist_orderby_dateline'),
        array('replies', 'threadlist_orderby_replies'),
        array('views', 'threadlist_orderby_views'),
        array('heats', 'threadlist_orderby_heats'),
        array('recommends', 'threadlist_orderby_recommends'),
    ),
    'default' => 'lastpost'
),

修改为

'orderby' => array(
    'title' => 'threadlist_orderby',
    'type'=> 'mradio',
    'value' => array(
        array('lastpost', 'threadlist_orderby_lastpost'),
        array('dateline', 'threadlist_orderby_dateline'),
        array('replies', 'threadlist_orderby_replies'),
        array('views', 'threadlist_orderby_views'),
        array('heats', 'threadlist_orderby_heats'),
        array('recommends', 'threadlist_orderby_recommends'),
        array('rand', '随机排序'),
    ),
    'default' => 'lastpost'
),

 

3.打开/source/class/block/forum/block_thread.php,找到代码

$orderby = isset($parameter['orderby']) ? (in_array($parameter['orderby'],array('lastpost','dateline','replies','views','heats','recommends')) ? $parameter['orderby'] : 'lastpost') : 'lastpost';
$lastposter = !empty($parameter['lastposter']) ? $parameter['lastposter'] : '';

修改为

$orderby = isset($parameter['orderby']) ? (in_array($parameter['orderby'],array('lastpost','dateline','replies','views','heats','recommends','rand')) ? $parameter['orderby'] : 'lastpost') : 'lastpost';
$lastposter = !empty($parameter['lastposter']) ? $parameter['lastposter'] : '';

 

4.打开/source/class/block/forum/block_thread.php,找到代码

$query = DB::query("SELECT DISTINCT t.*$sqlfield
FROM `".DB::table('forum_thread')."` t
$sqlfrom WHERE {$maxwhere}t.readperm='0'
$sql
AND t.displayorder>='0'
ORDER BY t.$orderby DESC
LIMIT $startrow,$items;"
);

修改为

if($orderby=='rand'){
$query = DB::query("SELECT DISTINCT t.* $sqlfield FROM `".DB::table('forum_thread')."` t $sqlfrom WHERE {$maxwhere}t.readperm='0' $sql AND t.displayorder>='0' ORDER BY rand() LIMIT $startrow,$items;");
}else{
$query = DB::query("SELECT DISTINCT t.*$sqlfield
FROM `".DB::table('forum_thread')."` t
$sqlfrom WHERE {$maxwhere}t.readperm='0'
$sql
AND t.displayorder>='0'
ORDER BY t.$orderby DESC
LIMIT $startrow,$items;"
);
}

就是在数据查询的外层加上了判断,如果是随机排序,查询里排序条件就用ORDER BY rand(),否则按原本的排序条件。

 

相关标签:

发布评论