wordpress实现最近访客小工具

Home / Article MrLee 2015-5-14 3090

在使用wordpress的侧边栏中很多同学会发现有的wordpress有这样一个功能

QQ截图20150514163955

其实就是统计最近来的人员数,这个功能比较不错的,这其实是多说的一个功能,但是多说是登陆用户,有数据可以操作。 原生wp函数就无法做到了,所以只能退居其次,调用最近留言的访客,当然,每个人只会显示一次。 首先在主题下面的functions.php添加以下代码
//读者墙
function zsofa_most_active_friends($friends_num = 10) {
    global $wpdb;
    $counts = $wpdb->get_results("SELECT * FROM (SELECT * FROM $wpdb->comments) AS tempcmt GROUP BY comment_author_email ORDER BY comment_date DESC LIMIT $friends_num");
    foreach ($counts as $count) {
    $c_url = $count->comment_author_url;
    if ($c_url == '') $c_url = get_bloginfo('url');
    $mostactive .= '
  • ' . '' . get_avatar($count, 58) . '
  • '; } return $mostactive; }
    原主题这个小工具是显示不了最近访客的,百度一下看到歧路亡羊博客里面有介绍实现,但是不能抓取到QQ或者其它新浪的头像。于是我自己整了一套,就是上面的代码。 接下来是小工具代码:
    //读者墙reader
    class readerWidget extends WP_Widget {
    	 function readerWidget(){
    	  $widget_ops = array('classname'=>'widget_reader','description'=>'侧边栏显示读者墙');
    	  $control_ops = array('width'=>250,'height'=>300);
    	  $this->WP_Widget(false, '[FO]读者墙', $widget_ops, $control_ops);
    	 }
    	 
    	 function form($instance){
    	 $instance = wp_parse_args((array)$instance,array('title'=>'读者墙','readerNum'=>'24'));//默认值
    	 $title = htmlspecialchars($instance['title']);
    	 $readerNum = htmlspecialchars($instance['readerNum']);
    	 echo '

    '; echo '

    '; } function update($new_instance,$old_instance){ $instance = $old_instance; $instance['title'] = strip_tags(stripslashes($new_instance['title'])); $instance['readerNum'] = strip_tags(stripslashes($new_instance['readerNum'])); return $instance; } function widget($args, $instance){ extract($args); $title = apply_filters('widget_title', empty($instance['title']) ? __('活跃读者','yang') : $instance['title']);//小工具前台标题 $title = $title; $readerNum = $instance['readerNum']; echo $before_widget; //id开始框 if( $title ) echo $before_title . $title . $after_title; //标题 if (function_exists('zsofa_most_active_friends')) { echo "
      " . zsofa_most_active_friends($readerNum) . "
    ";} ; echo $after_widget; //框架结束 } }
    最后还是在functions.php中注册这个小工具。
    register_widget('readerWidget');
    OK了,大功告成!

    本文链接:https://www.it72.com/2894.htm

    推荐阅读
    最新回复 (0)
    返回