今天小编就使用DUX大前端主题关于首页评论自动排第一这个功能,很多网站上都已经有了,网上相关的源码也是很多,这里讲一下通过DUX大前端上实现的过程。
先放张前端显示图片看下效果:
核心模板文件
新建一个名为 module_autofirst_by_comment.php 的文件,并将以下代码复制到该文件中:
comments where comment_ID in (select max(comment_ID) from $wpdb->comments where comment_approved='1' and comment_author_url !='' and user_id='0' GROUP BY comment_author_email) ORDER BY comment_date DESC LIMIT $num";
$user_info = $wpdb->get_results($user_sql);
foreach ($user_info as $user_info_li){
$user_info_lis= "
";
$out_user_info.= $user_info_lis;
}
$out_user_info = "
".QGG_options('qgg_autofirst_by_comment_title')."
- ".$out_user_info."
";
echo $out_user_info ;
}
autofirst_by_comment(QGG_options('qgg_autofirst_by_comment_num'))
?>
上述代码是实现此功能的核心代码,请确保代码复制准确无误。然后将代码丢到主题的 modules 文件夹中即可,当然丢到其他文件中也可以,只要后面显示代码中路径引用正确即可。
后台选项代码。
将以下代码复制到主题的 options.php 文件中即可:
// 首页评论自动排第一
$options[] = array(
'name' => __('首页评论自动排第一', 'QGG'),
'id' => 'qgg_autofirst_by_comment_open',
'std' => true,
'desc' => __('开启', 'QGG'),
'type' => 'checkbox');
$options[] = array(
'name' => __('首页评论自动排第一-标题', 'QGG'),
'id' => 'qgg_autofirst_by_comment_title',
'std' => __('评论自动排第一', 'QGG'),
'type' => 'text');
$options[] = array(
'name' => '显示评论者数量',
'desc' => '设置需要显示的个数。不明白?点击这里 进行留言。',
'id' => 'qgg_autofirst_by_comment_num',
'std' => 25,
'class' => 'mini',
'type' => 'text');
前端显示代码
将以下代码丢到主题的 index.php 文件中去,具体位置大家根据个人喜好选择即可。
代码主要是用于判断后台是否开启了“首页评论自动排第一”功能,并调取对应的文件,注意代码中文件的位置应与你核心代码的位置一致。
CSS样式美化
添加完成后前端显示效果应该不符合我们要求,将以下代码复制到主题 main.css 文件中即可,当然你也可以自由调整样式。
/** 首页评论自动排第一 */
.autofirst-div{
background: #FFF;
padding: 15px 20px;
margin: 10px 0px;
border-radius: 4px;
}
.autofirst-li {
width: 20%;
height: 28px;
line-height: 20px;
display: inline-block;
padding: 3px 9px;
color: #FFF;
overflow: hidden;
}
@media (max-width:640px){
.autofirst-div{
display: none;
}
.autofirst-title{
display: none;
}
}