WordPress最近访客小工具纯代码实现方法
经过不断的努力,每天更新文章,收录越来越多,我的博客访问人数也越来越多了。
当初看到别人博客都有最近访客的效果,感觉好厉害。
我想我的博客也应该实现这样的功能,下面是我的博客效果:

多说一直有一个最近访客的小工具,显示最近访问的访客列表,比较不错,但是多说是登陆用户,有数据可以操作。
原生wp函数就无法做到了,所以只能退居其次,调用最近留言的访客,当然,每个人只会显示一次。
把下面代码放到functions.php中:
//获取最近读者
function visitors($tim,$lim){
global $wpdb;
$query="SELECT COUNT(comment_ID) AS cnt, comment_author, comment_author_url, comment_author_email FROM (SELECT * FROM $wpdb->comments LEFT OUTER JOIN $wpdb->posts ON ($wpdb->posts.ID=$wpdb->comments.comment_post_ID) WHERE comment_date > date_sub( NOW(), INTERVAL $tim day ) AND comment_author_email != '' AND post_password='' AND comment_approved='1' AND comment_type='') AS tempcmt GROUP BY comment_author_email ORDER BY comment_date DESC LIMIT $lim";
$wall = $wpdb->get_results($query);
foreach ($wall as $comment)
{
if( $comment->comment_author_url )
$url = $comment->comment_author_url;
else $url="#";
$r="rel='external nofollow'";
$tmp = "上面是核心函数,然后需要做成小工具就可以了:
//注册 Widget 小工具
add_action('widgets_init', create_function('', 'return register_widget("mk_visitors");'));
class mk_visitors extends WP_Widget {
//注册一个WordPress小工具
function mk_visitors(){
$this->WP_Widget('mk_visitors', '读者墙', array( 'description' => '显示近期评论最多的读者头像' ));
}
//前端显示小工具
function widget($args, $instance) {
extract($args, EXTR_SKIP);
echo $before_widget;
$title = apply_filters('widget_name', $instance['title']);
$limit = $instance['limit'];
$timer = $instance['timer'];
echo $before_title.$title.$after_title;
echo '- ';
echo visitors($tim=$timer, $lim=$limit );
echo '
上面的说明已经很仔细了,然后就是样式的问题了,这个可以自由发挥,下面是明凯博客的样式了:
.sidebar ul {
list-style: none;
margin: 1.5em 0;
padding: 0;
}
.sidebar ul li {
margin: 0;
padding: 5px 0;
border-top: 1px solid #ddd;
color: #969696;
}
.visitors li{float:left;}
其实大部分是主题自带的。
用border-radius做圆和奥运五环的的效果 SQL对汉字月份进行排序的两种方法
Thanks for sharing. I read many of your blog posts, cool, your blog is very good.
Thanks for sharing. I read many of your blog posts, cool, your blog is very good. https://accounts.binance.com/en-IN/register-person?ref=A80YTPZ1
Can you be more specific about the content of your article? After reading it, I still have some doubts. Hope you can help me.
logo 是怎改的