我們常常會看到一些站長寫的博客會有一個最新訪客功能了,他們可以非常大氣的這個功能好復雜了,我后來百度一搜索發現了有很多相關文章了,下面來給各位整理一下方法.
原生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;}
其實大部分是主題自帶的.
新聞熱點
疑難解答
圖片精選