DUX主题添加首页评论自动排第一功能

关于首页评论自动排第一这个功用,很多网站上都曾经有了,网上相关的源码也是很多,这里只简单记载一下本人博客(DUX主题)上完成的过程。

图片[1]-DUX主题添加首页评论自动排第一功能-孤勇者社区

先放张前端显现图片看下效果:

图片[2]-DUX主题添加首页评论自动排第一功能-孤勇者社区

修正主题前必看>>> 关于本博“主题修正”你需求晓得的一些事 。

中心模板文件

新建一个名为 module_autofirst_by_comment.php 的文件,并将以下代码复制到该文件中:

<?php 
/* 首页最新评论自动排第一
 * 蝈蝈要安静——一个不学无术的伪程序员
 * https://blog.quietguoguo.com
*/

function autofirst_by_comment($num){
    global $wpdb;
    $user_sql="SELECT comment_author, comment_author_url, comment_date from $wpdb->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= "<li class='autofirst-li'><i class='fa fa-yelp' aria-hidden='true' style='color:#999; font-size:12px; '></i>&nbsp;&nbsp;<a target=\"_blank\" href=\"".$user_info_li->comment_author_url."\">".$user_info_li->comment_author."</a></li>";
        $out_user_info.= $user_info_lis;
    }
    $out_user_info = "<div class='title autofirst-title'><h3>".QGG_options('qgg_autofirst_by_comment_title')."</h3></div><div class='autofirst-div' ><ul>".$out_user_info."</ul></div>";
    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 文件中去,详细位置大家依据个人爱好选择即可。

<!-- 首页最新评论自动排第一 -->
<?php  
if( QGG_options('qgg_autofirst_by_comment_open') ){
    include get_stylesheet_directory() . '/modules/module_autofirst_by_comment.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;
    }
}

本文中心代码来源于网络,这里至简单记载了本博完成该功用的方式,其他主题完成方式相似,大家参照中心代码修正调整即可。

资源下载

免费资源本公开载点击下载百度网清点击下载提取码: IQGG复制留意:本站资源多为网络搜集,如触及版权问题请及时与站长联络,我们会在第一时间内与您协商处理。如非特殊阐明,本站一切资源解压密码均为:zibuyu.life。

------本页内容已结束,喜欢请分享------

感谢您的来访,获取更多精彩文章请收藏本站。

© 版权声明
THE END
喜欢就支持一下吧
点赞9赞赏 分享
评论 共1条
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片
    • 头像时卡0