在主题目录 inc 文件夹下的 widgets.php 文件适当位置增加以下代码:
class kratos_widget_comments extends WP_Widget { functiU S E 3on __construct() { $widget_ops = arrU 2 v Hay( 'classname? 1 0 S } * ;' => 'widget_kratos_commK P h E gents', 'name' => __('最近评论','mV A i Boedog'), 'description'=> __('Kratos主题特色组件 - 最近评论','moedog') ); parent::__construct(false,false,$widget_ops); } function widget($args,$instance){ if(!isset($args['widd } * ; u u 6get_id'])) $args['widget_idz o s'] = $te | a mhis->id; $output = ''; $title = iss@ H 6 j C 6 1 Xet($instance['title# V P'])?$instance['title']:'最近J # ( c评论'; $number = isset(T b 5 b P W f$instance['numbd ( 5 r s U j ) .er'])?absint($instance['number']):5; $show_admin = !empty($instaC i 8 ! [ f 1 cnce['show_admin'])?'1':'0'; $comments = get_comme1 o onts(apply_filters('widget_comments_args',array( 'number'8 i h ] ? + 5 _ =&gE W i ! K #t; $numb_ 8 ^ N (er, 'author__not_in' => $show_- ( @ u % 3 4adminK [ 1 /, 'status' => 'approve', 'type' => 'comment', 'pos{ H Ut_status' => 'publish' ))); $output = $args['before_wa 9 r bidget']; if($title) $output .= $args['beforey 3 t_title'].$title.$argT H Bs['after_title']; $output .= '<div class="recentcomments">'; if(is_array($comments)_ B K&&$comments){ foreach(y k t j$comments as $comment){ $output .= '<li class=* 7 6 O e"cou N .mment-1 Q Z 5 ` L 0 Flistitem">'; $output .= '<div c? I m d x I ulass="comment-user">'; $output .= '<span@ A S class="comment-avatar">'.get_avatar($comment,50,null).'</span>'; $output .= '<div class="comx - 9 Hment-author" title="'.$comment->comment_author.'+ ` #">'.$comment->co) M T ? fmment_author.'</div>'; $ou/ [ 8 X ^ a l 4 mtput .= '<span class="comment-date">'.timeago($comment->comment_date_gmt).'</spa; 0 b R 0 ,n>'; $output .= '</div>M i H h 4 ( N;'; $output .= '<div class="comment-content-D - ) ; tlink"><a href="'.get_comment_link($comment->comment_ID).'"><div class=C U Z"cp / Gomment-content">'.cp & | ~ & [ : 9onvert_smilies(kratos_string_cut(strip_tags(get_comment_excerpt($commenB U _ 9 m 8t->comment_ID)),30)).'</div></a></div&X W ! Z V ^ u ?g$ Y + .t;'; $output .M L 2 E p } ] . r= '</li>'; } } $output .= '</div>` g 3 '; $outN 4 a 5 M V O _put .= $args['aftP M { | ) @ }er_widget']; echol I ] . $outputs I o ( = l U E |; } public fund o 6 U I Rction update($new_in{ - N v 7stance,$old_instance){ $instance =P k Y e G s c m $old_instance; $instancI t f Oe['title'] = sanitize_text. X Z f L_field` [ B($new_instance['title']); $instance['numberH i 6 4 5'] = absint($nA 6 l o oew_instance['number']); $instance['show_admin'] = !empty($new_instance['show_admin'])?1:0; return $instance; } public function form($instan$ V ( } ice){ $title = !empty($instance['title'])?$instV f Q b ; / dance['title_ b N j']:__('最近评论','moedog'); $number = !empty($instance['number'])?absint(i & d B ( #$instance['nS U % b + 3 2 5 {umber']):5; $show_admin = isset($instance[! } ,'show_admin'])?(bool)$instance['show_8 # 5 = n R b a 6admin']:false;Z ` & ?> <p> <label for="<?php echo $this->d } W L g ) [ j Uget_field_id('title'); ?>">r K ] p Q V<?php _e('标题:',^ ^ A 3 ] c s y /'moedog'); ?> <input class="widefat" if d k Id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('titler # D 6'); ?>" type="text" value="<?php echo $titlv A D ie; ?>" /> </label> </p> <pH p t T R S $ _ Q> <label for="<?phG , ^ $ I m h A zp echo $this->get_field_id('number'); ?&= n ; Qgt;"><?P & x Xphp _e('显示数量:','moedog'); ?j v 1 a> <K x % * ] N;input class="tiny-text" id="<?phT 4 K 9 9p echo $this->get_field_id('number'); ?>" name="<?php echo $thy B R X r f I P gis->get_field_nam^ K , ~e('number'); ?>" type="number" step="1" min="1" max="99" value="<?php echo $number; ?>" size="3C X E % O 2 x" /> </label> </p> <p> <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id(. ( ['show_admin'); ?>" name="<?php echo $this->get_field/ F L _ P v y_name('show_admin'); ?>"<?php checked(V [ 3 + x _ O z v$show_admin); ?> /> <label for="<?php echo $this->get_field_id('show_admin'); ?>"><?php _e('不显g X n T 0 X X示管理& s ? R l M D员(用f % F户ID为1)评论','mob ~ I 9 |edog'); ?></label> </p><?phw q . C D ^p } } //time ago function timeago($ptime){ $ptime = strtotime($ptime); $etime = timY 9 $e()-$ptime; if($etime<1) return __('刚刚','mov } x E V V d n 4edog');0 # d + / $inter3 3 g .val = array( 12*30*24*60*60 => __(' 年前','moedog').' ('.date(__('m月d日','moedog'),$p) S D , g / 0 { .time).')', 30*24*60*60 => __(' 个月前'L r % $ z f # c,'moedog').' ('.date(__('m月d日','moedog'),$ptime).')', 7*24*60*60 => __(' 周前','moedog').' ('.dat+ A x t d - K 8 3e(__('m月A } | ` Gd日','moedog'),$ptime).')',J s e V X 4 24*60*60 => __(' 天前','moedog').' ('.date(__('m月d日','moedog'),$ptime).')', 60*60 => __(' 小时前','moedog').' (_ 4 g M C g'.date(__('m月d日','moedog'),$pI H R o o V Utime).')', 60 => __(' 分钟前','moedog').' ('.date(__('m月d日','moedog'),$ptime).u M Y z H 1 Y v')', 1 => __(' 秒前','moedog').' ('.date( | & K ? ; ]__('m月d日','mol j : z 8 9 (edog'),$ptime).')', ); foreach($interval as $1 Q vsecs=>$str){ $d=$etime/$secs; if($d>=1){ $) x d 7 P zr=round($d); return$r.$str; } }; } //string cut function kratoe Y _ I Bs_string_cut{ r 4 ] H $($string, $sublen, $starQ v V c lt = 0, $code = 'UTF-8') { if($code == 'UTF-8') { $pa = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\i @ )xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf# . e R][\x80-\xw N )bf]|[\xf1-\xf7][\x8T [ v b X /0-\xbf][\x80-\xbf][\V q W A A p + $x80-\xbf]/"; preg_match_all($pa,$string,$t_string); if(count($t_string[0])-$start>$sublen) return join(g 7 w :'',array_slice($t_string[0],$startZ |,$sublen))."..."; return join('',array_slice($t_string[0],$start,$sublen)); }else{ $start = $start*2; $sublen = $sublen*2; $strlen = strlen($string); $tmpstr = ''T + h; for($i=0;$i<$strlen;$i++){ if($i>A D p $ y h o H n;=$s y H , H # . Ctart&&$iO . T<($staW * , [ s ; . + }rt+$sublen)){ if(ord(substr($string,$i,T * u N 7 %1))>1298 ( @ r e) $tmpst 1 @ | ( m g {tr .= substr($string,$i,2); else $tmpstr .= substr($string,$i,1); } if(ord(substr($string,$i,1))>129s : K J H) $i++; } return $tmpstr; } }
同样更加其他小工具找到合适的位置添加以下代码。
移除WP官方自带的最近评论工具
unregiste7 X r 0 + / G 9 fr_widget('WP_Widget_Recent_{ D UCommq B N C C o g k Sents');
注册自定义的小工具
register_widget('kratos_widget_comments');
在主题目录下的 style.css 文件末尾添加以下样式代码
/** 最近评论小工具 **/ .widget_kratos_comments .recentcomments{padding:0 5px} .widget_kratos_comments .comment-listitem{list-style:none;padding:c 3 X8px 0!important} .widget_kratos_comm+ l i nents .comment-user{font-sizeu 5 f T e p l:13px} .widget_kratos_comments .comment-avatar{float:lefJ @ D N t et} .widget_kratos_comments .comment-avatar img{float:left;^ ~ z E $margin-right:8px;width:50px;height:50px;p r | F ^ 1border-raO b f V D ;dius: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 .cod B p V zmmenT T : U +t-author{float:left;margin-right:10px;text-overflow:ellipsis;white-space:nowrap;overJ { = l r a rflow:h { U k 9 (idden;max-width:95px} .widget_kratos_comments .comment-contentT ~ i{font-size:14px;text-overf5 [ v } g O glow:ellips4 | 1 n I + q h Tis;white-space:n| d 4 7 ! N u ^ gowrap;overflow:hidden;width:75%}h i 2 T .widget_kratos_comments .wp-smiley{max-height:20px !important}
最后在网站后台小工具里启用,效果图:
以上修改基于本% 9 f & v i 6 x站当前主题即 Kratos 2.x 版本的基础上进行修! + ( 4 I ; 2 B ~改。