最近给博客添加了一个显示作者信息的小工具,网上也有很多关于边栏显示作者信息的实现方式,但是大多都需要我们再修改代码中的一些信息,很是麻烦,于是自己借鉴了很多网上的代码并优化成了目前我所使用的这款显示作者信息小工具。
对于社交信息的获取,由于并不是所有主题都会有微博、QQ、微信等信息的获取,这里我只获取了用户信息中的博客地址及作者博文的内容,当然你也可以通过增加这些字段来实现更多社交媒体的选项。具体增加方式以增加微博社交信息为例,在function.php文件中添加如下代码:
1
2
3
4
5
6//自定义用户信息小工具
functionauthor_info_methods($contactmethods){
$contactmethods['weibo']='微博';
return$contactmethods;
}
add_filter('user_contactmethods','author_info_methods',10,1);
下面是小工具前端显示的截图,希望你喜欢。
好了,现在我们来看下作者信息小工具的具体实现。
新增 widget-authorinfo.php 文件
新建一个 widget-authorinfo.php 的文件,并在其中添加如下代码:
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109<?php
/*
Widget Name:作者信息
Description:显示当前文章的作者信息
Version:1.0
*/
//DUX主题直接使用此代码即可,其他主题可能需要使用下面的代码注册小工具
//add_action('widgets_init', create_function('', 'return register_widget("widget_ui_authorinfo");'));
classwidget_ui_authorinfoextendsWP_Widget{
functionwidget_ui_authorinfo(){
$widget_ops=array('description'=>'显示当前文章的作者信息!');
$this->WP_Widget('widget_ui_authorinfo','作者信息',$widget_ops);
}
functionupdate($new_instance,$old_instance){
return$new_instance;
}
functionwidget($args,$instance){
extract($args);
echo$before_widget;
echowidget_ui_authorinfo();
echo$after_widget;
}
}
//获取作者所有文章浏览量
if(!function_exists('author_posts_views')){
functionauthor_posts_views($author_id=1,$display=true){
global$wpdb;
$apvn="SELECT SUM(meta_value+0) FROM $wpdb->posts left join $wpdb->postmeta on ($wpdb->posts.ID = $wpdb->postmeta.post_id) WHERE meta_key = 'views' AND post_author = $author_id ";
$author_posts_views=intval($wpdb->get_var($apvn));
if($display){
echonumber_format_i18n($author_posts_views);
}else{
return$author_posts_views;
}
}
}
//获取作者参与评论的评论数
if(!function_exists('author_posts_comments')){
functionauthor_posts_comments($author_id=1,$author_email='',$display=true){
global$wpdb;
$apcn="SELECT count(comment_author) FROM $wpdb->comments WHERE comment_approved='1' AND comment_type='' AND (user_id = '$author_id' OR comment_author_email='$author_email' )";
$author_posts_comments=intval($wpdb->get_var($apcn));
if($display){
echonumber_format_i18n($author_posts_comments);
}else{
return$author_posts_comments;
}
}
}
functionwidget_ui_authorinfo(){
?>
/img/author-img.jpg);">
"title="<?phpthe_author ();?>"class="widget_avatar">
"class="author-ident author1">