带头像的 "近期评论" 小工具

最近有点想换主题了呃···~~~···自己改的地方有点多,主题改动了,源文件也改动了,3.8.1的安全更新差点都不敢随意升级。。所以我们尽量不要改源文件好。但总有的地方自己不满意,比如这个“近期评论”小工具:默认的不显示评论内容(这也叫近期评论),而且把作者的回复也都输出,还不带头像······好吧,操刀——

小工具嘛,网上的方法都是直接去改 /wp-includes/default-widgets.php :参考《 改进WordPress侧边栏最新评论功能:不显示自己(作者)的评论+直接显示评论内容 》。

其实我们可以自己写个小工具把按照上面这个链接的教程改后的内容作为我们小工具的内容,这样就不需要改动源文件了,下次WP升级就大胆的升吧!
不过因为小工具都是继承自WP_Widget 的类,他们本身也是class,所以我们可以少写点,直接继承自WP_Widget_Recent_Comments这个默认的近期评论类。

先来效果图:

近期评论小工具带头像-效果图
近期评论小工具带头像

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/** '近期评论'()
 *  http://youthlin.com/?p=610 
 */
class My_Widget_Recent_Comments extends WP_Widget_Recent_Comments {
//WP_Widget_Recent_Commentsfunction widget( $args, $instance )
    function My_Widget_Recent_Comments() {
    //
    $widget_ops = array('classname' => 'my_widget_recent_comments', 'description' => __('显示最新评论内容'));
    $this->WP_Widget('my-recent-comments', __('近期评论(带头像)', 'my'), $widget_ops);
    }
    function widget( $args, $instance ) {
    //
    global $comments, $comment;
    $cache = wp_cache_get('widget_recent_comments', 'widget');
    if ( ! is_array( $cache ) )
	$cache = array();
    if ( ! isset( $args['widget_id'] ) )
	$args['widget_id'] = $this->id;
    if ( isset( $cache[ $args['widget_id'] ] ) ) {
	echo $cache[ $args['widget_id'] ];
	return;
    }
    extract($args, EXTR_SKIP);
    $output = '';
    $title = ( ! empty( $instance['title'] ) ) ? $instance['title'] : __( 'Recent Comments' );
    $title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
    $number = ( ! empty( $instance['number'] ) ) ? absint( $instance['number'] ) : 5;
    if ( ! $number )
 	$number = 5;
////////////////////==========////////////////////////=================================================
    $comments = get_comments( apply_filters( 'widget_comments_args', array( 'number' => $number, 'status' => 'approve', 'post_status' => 'publish' , 'user_id' => 0) ) );
    $output .= $before_widget;
	if ( $title )
	  $output .= $before_title . $title . $after_title;
	$output .= '<ul id="recentcomments">';
	if ( $comments ) {
	  // Prime cache for associated posts. (Prime post term cache if we need it for permalinks.)
	  $post_ids = array_unique( wp_list_pluck( $comments, 'comment_post_ID' ) );
	  _prime_post_caches( $post_ids, strpos( get_option( 'permalink_structure' ), '%category%' ), false );
	  foreach ( (array) $comments as $comment) {
////////////////////======foreach里面 =====================================
		$avatar = get_avatar($comment,34,'','头像');
////////////////////==========/////////////////////////================================================
		$output .=  '<li class="recentcomments">' . /* translators: comments widget: 1: comment author, 2: post link */
		sprintf(_x('%1$s : %2$s', 'widgets'),$avatar . get_comment_author_link(), '<a href="'
		//,
		. esc_url( get_comment_link($comment->comment_ID) ) 
		. '" title="'.get_the_title($comment->comment_post_ID).','.get_comment_date(Y-m-j)
		// http://ifonder.com/2012/04/13/change-recent-comments-widget/
		. '">'
		. mb_strimwidth(strip_tags($comment->comment_content),0,34, '...>>') . '</a>') . '</li>';
		}
 	  }
	/*
	http://www.typemylife.com/wordpress-recent-comments-remove-author-name-display-content/
	:
	(_x('%1$s on %2$s', 'widgets')on
	
	get_the_title($comment->comment_post_ID)mb_strimwidth(strip_tags($comment->comment_content),0,50, )
	50"。。。》"
	 (:)
	ID+""+
	2
	default-widgets.php文件
	
	$comments = get_comments( apply_filters( 'widget_comments_args', array( 'number' => $number, 'status' => 'approve', 'post_status' => 'publish' ) ) );
	
	$comments = get_comments( apply_filters( 'widget_comments_args', array( 'number' => $number, 'status' => 'approve', 'post_status' => 'publish', 'type' => 'comment', 'user_id' => 0 ) ) );
	'user_id' => 0'type' => 'comment'pingback和trackback类留言
		*/
//typemylife.com那里学的代码
//便 :) 
	$output .= '</ul>';
	$output .= $after_widget;

	echo $output;
	$cache[$args['widget_id']] = $output;
	wp_cache_set('widget_recent_comments', $cache, 'widget');
	}
}
register_widget('My_Widget_Recent_Comments');

主要参考文章:

说明:
  • get_comment_link()—— 是获取评论的链接地址,其实就是文章地址加上‘#comment-评论ID’;
  • mb_strimwidth(str,0,34,”…”)——这个是最关键,截取字符。参数一是要截取的源内容,参数二三是始末位置,参数四是如果截取后源内容还有剩就用参数四添加在输出后面。
  • get_avatar($comment,’32′)——有四个参数( 官方文档),第一是邮件地址或用户id,第二是图片大小,第三是默认图像地址,第四是头像Alt标签。通过第三个参数可以修改没有头像的用户的米哦人头像哦!

Reader Echoes

29 comments

  • #1078从良未遂
    以前找个这样的教程找不到,现在主题自带了你就写出来了 白眼
    • #1079Youth.霖
      回复 @从良未遂啊哈?主题自带头像了啊,我没发现呢。这是14年写的那时还没有啊。这个方法可以自定义要显示的内容呢
  • #1077晨曦
    感觉好乱 调皮
  • #1076百家乐
    鼓掌 谢谢楼主的分享,很不错的东东、
  • #1075太阳城
    厉害
  • #1073wingsBlog
    现在看到有点晕
    • #1074Youth™霖
      回复 @wingsBlog调皮 亲,其实就4个函数,照着写就没问题的~
  • #1072微历史
    感觉加了头像反而不好看了
  • #1071烂番茄
    坏笑 嘿,我看到我了!
  • #1070tiandi
    默认评论真的是傻蛋,老外不知怎么想的。
  • #1069互传站长网
    学习了,不过我没有使用这个!
  • #1067郑杰
    css文件你真不该放在七牛。。。七牛的域名解析有些慢,让first byte延迟了。css应该就放在网站同域名下 坏笑 坏笑 坏笑
    • #1068Youth™霖
      回复 @郑杰哦!原来空间在美国,等下设置下把css弄回来。first byte? 疑问
  • #1064小草元
    继承类,很好。我也曾有评论这样改的想法,终没有去碰它。
    • #1065Youth™霖
      回复 @小草元这两天折腾后发现,小工具也是可以自己写的 得意 ,这两天搜索了一些文章,旁边的说说就是自己写的小工具(以前是直接加在sidebar.php 撇嘴
    • #1066Youth™霖
      回复 @小草元有想法就试试呗,先在本地调试,一步一步来,一不小心就弄好了 呲牙
  • #1062逗妇乳
    <a href="http://doufu.ru/minty-improved-version-available-recent-comments-widget.html" title="Minty可用改进版最近评论小工具" target="_blank" rel="nofollow noopener noreferrer ugc">http://doufu.ru/minty-improved-version-available-recent-comments-widget.html</a> 我在用这个
    • #1063Youth™霖
      回复 @逗妇乳呲牙 哈哈,我也是搜索的,没想到你早就写了(或许我以前看到了但忘了 发呆
  • #1060PHP二次开发
    你网站改的越来越漂亮了。
  • #1057小菜
    厉害,支持个,感觉头像小点好看些
  • #1056斌果
    还不如新加一个小工具
  • #1055音乐天堂
    一直在学习,始终在努力
  • #1054抗战30年
    等我换回wordpress的原生评论了,再来看一次。
  • #1053明升M88开户
    调皮 赞一个 支持博主
  • #1051c
    发现博主是个PHP大神…… 膜拜……
    • #1052Youth™霖
      回复 @c我可不是,你的博客程序我都看不懂,我只是谷歌常用的wordpress函数瞎写的

表情

评论提交后需经审核才会显示。