美化侧边栏最近评论小工具

在主题目录 inc 文件夹下的 widgets.php 文件适当位置增加以下代码:

class kratos_widget_comments extends WP_Widget {
    function __construct() {
        $widget_ops = array(
            'classname'  => 'widget_kratos_comments',
            'name'       => __('最近评论','moedog'),
            'description'=> __('Kratos主题特色组件 - 最近评论','moedog')
        );
        parent::__construct(false,false,$widget_ops);
    }
    function widget($args,$instance){
        if(!isset($args['widget_id'])) $args['widget_id'] = $this->id;
        $output = '';
        $title = isset($instance['title'])?$instance['title']:'最近评论';
        $number = isset($instance['number'])?absint($instance['number']):5;
        $show_admin = !empty($instance['show_admin'])?'1':'0';
        $comments = get_comments(apply_filters('widget_comments_args',array(
            'number' => $number,
            'author__not_in' => $show_admin,
            'status' => 'approve',
            'type' => 'comment',
            'post_status' => 'publish'
            )));
        $output    = $args['before_widget'];
        if($title) $output .= $args['before_title'].$title.$args['after_title'];
        $output .= '<div class="recentcomments">';
        if(is_array($comments)&&$comments){
            foreach($comments as $comment){
                $output .= '<li class="comment-listitem">';
                $output .= '<div class="comment-user">';
                $output .= '<span class="comment-avatar">'.get_avatar($comment,50,null).'</span>';
                $output .= '<div class="comment-author" title="'.$comment->comment_author.'">'.$comment->comment_author.'</div>';
                $output .= '<span class="comment-date">'.timeago($comment->comment_date_gmt).'</span>';
                $output .= '</div>';
                $output .= '<div class="comment-content-link"><a href="'.get_comment_link($comment->comment_ID).'"><div class="comment-content">'.convert_smilies(kratos_string_cut(strip_tags(get_comment_excerpt($comment->comment_ID)),30)).'</div></a></div>';
                $output .= '</li>';
            }
        }
        $output .= '</div>';
        $output .= $args['after_widget'];
        echo $output;
    }
    public function update($new_instance,$old_instance){
        $instance = $old_instance;
        $instance['title'] = sanitize_text_field($new_instance['title']);
        $instance['number'] = absint($new_instance['number']);
        $instance['show_admin'] = !empty($new_instance['show_admin'])?1:0;
        return $instance;
    }
    public function form($instance){
        $title = !empty($instance['title'])?$instance['title']:__('最近评论','moedog');
        $number = !empty($instance['number'])?absint($instance['number']):5;
        $show_admin = isset($instance['show_admin'])?(bool)$instance['show_admin']:false; ?>
        <p>
            <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('标题:','moedog'); ?>
                <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" />
            </label>
        </p>
        <p>
            <label for="<?php echo $this->get_field_id('number'); ?>"><?php _e('显示数量:','moedog'); ?>
                <input class="tiny-text" id="<?php echo $this->get_field_id('number'); ?>" name="<?php echo $this->get_field_name('number'); ?>" type="number" step="1" min="1" max="99" value="<?php echo $number; ?>" size="3" />
            </label>
        </p>
        <p>
            <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('show_admin'); ?>" name="<?php echo $this->get_field_name('show_admin'); ?>"<?php checked($show_admin); ?> />
            <label for="<?php echo $this->get_field_id('show_admin'); ?>"><?php _e('不显示管理员(用户ID为1)评论','moedog'); ?></label>
        </p><?php
    }
}

//time ago
function timeago($ptime){
    $ptime = strtotime($ptime);
    $etime = time()-$ptime;
    if($etime<1) return __('刚刚','moedog');
    $interval = array(
        12*30*24*60*60 => __(' 年前','moedog').' ('.date(__('m月d日','moedog'),$ptime).')',
        30*24*60*60 => __(' 个月前','moedog').' ('.date(__('m月d日','moedog'),$ptime).')',
        7*24*60*60 => __(' 周前','moedog').' ('.date(__('m月d日','moedog'),$ptime).')',
        24*60*60 => __(' 天前','moedog').' ('.date(__('m月d日','moedog'),$ptime).')',
        60*60 => __(' 小时前','moedog').' ('.date(__('m月d日','moedog'),$ptime).')',
        60 => __(' 分钟前','moedog').' ('.date(__('m月d日','moedog'),$ptime).')',
        1 => __(' 秒前','moedog').' ('.date(__('m月d日','moedog'),$ptime).')',
    );
    foreach($interval as $secs=>$str){
        $d=$etime/$secs;
        if($d>=1){
        $r=round($d);
        return$r.$str;
        }
    };
}
//string cut
function kratos_string_cut($string, $sublen, $start = 0, $code = 'UTF-8') {
    if($code == 'UTF-8') {
        $pa = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/";
        preg_match_all($pa,$string,$t_string);
        if(count($t_string[0])-$start>$sublen) return join('',array_slice($t_string[0],$start,$sublen))."...";
        return join('',array_slice($t_string[0],$start,$sublen));
    }else{
        $start = $start*2;
        $sublen = $sublen*2;
        $strlen = strlen($string);
        $tmpstr = '';
        for($i=0;$i<$strlen;$i++){
            if($i>=$start&&$i<($start+$sublen)){
                if(ord(substr($string,$i,1))>129) $tmpstr .= substr($string,$i,2);
                else $tmpstr .= substr($string,$i,1);
            }
        if(ord(substr($string,$i,1))>129) $i++;
        }
        return $tmpstr;
    }
}

同样更加其他小工具找到合适的位置添加以下代码。
移除WP官方自带的最近评论工具

unregister_widget('WP_Widget_Recent_Comments');

注册自定义的小工具

register_widget('kratos_widget_comments'); 

在主题目录下的 style.css 文件末尾添加以下样式代码

/** 最近评论小工具 **/

.widget_kratos_comments .recentcomments{padding:0 5px}
.widget_kratos_comments .comment-listitem{list-style:none;padding:8px 0!important}
.widget_kratos_comments .comment-user{font-size:13px}
.widget_kratos_comments .comment-avatar{float:left}
.widget_kratos_comments .comment-avatar img{float:left;margin-right:8px;width:50px;height:50px;border-radius:50%;-webkit-box-shadow:0 .125rem 1.0625rem 0 rgba(0,0,0,.1);box-shadow:0 .125rem 1.0625rem 0 rgba(0,0,0,.1)}
.widget_kratos_comments .comment-author{float:left;margin-right:10px;text-overflow:ellipsis;white-space:nowrap;overflow:hidden;max-width:95px}
.widget_kratos_comments .comment-content{font-size:14px;text-overflow:ellipsis;white-space:nowrap;overflow:hidden;width:75%}
.widget_kratos_comments .wp-smiley{max-height:20px !important}

最后在网站后台小工具里启用,效果图:
《美化侧边栏最近评论小工具》
以上修改基于本站当前主题即 Kratos 2.x 版本的基础上进行修改。

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

  1. Lvtu 说道:

    大神,来个此款主题下增加评论者级别的教程撒。。。谢谢 :010:

    1. 黑鸟 说道:

      思路及其素材已经记录过了,不折腾了。