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_Comments,那么只需重写function 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');
|
Reader Echoes
29 comments