我们常常会看到一些站长写的博客会有一个最新访客功能了,他们可以非常大气的这个功能好复杂了,我后来百度一搜索发现了有很多相关文章了,下面来给各位整理一下方法.
原生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 = "<li><a href='".$url."' ".$r." title='".$comment->comment_author." 留下".$comment->cnt."条信息'>".get_avatar($comment->comment_author_email, 40)."</a></li>";
- $output .= $tmp;
- }
- echo $output ;
- }
上面是核心函数,然后需要做成小工具就可以了,代码如下:
- //注册 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 '<ul class="visitors">';
- echo visitors($tim=$timer, $lim=$limit );
- echo '</ul><div class="clear"></div>';
- echo $after_widget;
- }
- //保存小工具设置选项
- function update($new_instance, $old_instance) {
- $instance = $old_instance;
- $instance['title'] = strip_tags($new_instance['title']);
- $instance['limit'] = strip_tags($new_instance['limit']);
- $instance['timer'] = strip_tags($new_instance['timer']);
- return $instance;
- }
- //后台小工具表单
- function form($instance) {
- $instance = wp_parse_args( (array) $instance, array(
- 'title' => '最近读者',
- 'limit' => '15',
- 'timer' => '30'
- )
- );
- $title = strip_tags($instance['title']);
- $limit = strip_tags($instance['limit']);
- $timer = strip_tags($instance['timer']);
- echo '<p><label>标题:<input class="widefat" id="'.$this-/>get_field_id('title').'" name="'.$this->get_field_name('title').'" type="text" value="'.$instance['title'].'" /></label></p><p><label>显示数目:<input class="widefat" id="'.$this-/>get_field_id('limit').'" name="'.$this->get_field_name('limit').'" type="number" value="'.$instance['limit'].'" /></label></p><p><label>几天内:<input class="widefat" id="'.$this-/>get_field_id('timer').'" name="'.$this->get_field_name('timer').'" type="number" value="'.$instance['timer'].'" /></label></p>';
- }
- }
上面的说明已经很仔细了,然后就是样式的问题了,这个可以自由发挥,下面是明凯博客的样式了:
- .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;}
其实大部分是主题自带的.
新闻热点
疑难解答
图片精选