wordpress功能集成(十一):小工具的制作方法

wordpress使用侧边栏小工具,或者其它位置的小工具很是方便,但是默认的那几个小工具完全不够用,今天就讲解一下如何制作一个小工具,然后接下来的几篇教程,给出几个小工具的实例。

今天我们以制作一个按浏览量排布的热门文章小工具。

小工具有三个部分,一、后台显示,二、数据保存,三、前台显示。当然如果你的小工具不需要在后台设置什么数据,那数据保存可以省掉了。一般来讲,一个小工具至少应该有这三个部分。

第一步,在自己的主题中新建一个小工具文件,随便命名,比如我新建一个文件夹widget然后在里面新建一个文件hotposts.php。

小工具是一个类,像侧边栏一样,你还得用代码注册它,它在能在后台使用。基本代码代码基本上一成不变:

  1. <?php   
  2.   
  3. //定义小工具类AshuPostViews    
  4. class AshuPostViews extends WP_Widget{   
  5.        
  6.     function AshuPostViews(){   
  7.         //这是定义小工具信息的函数,也是类的构建函数   
  8.     }   
  9.        
  10.     function form($instance){   
  11.         //这是表单函数,也就是控制后台显示的   
  12.     }   
  13.        
  14.     function update($new_instance,$old_instance){   
  15.         //这是更新数据函数,小工具如果有设置选项,就需要保存更新数据   
  16.     }   
  17.        
  18.     function widget($args,$instance){   
  19.         //这是控制小工具前台显示的函数   
  20.     }   
  21.   
  22. }   
  23.   
  24.   
  25. function AshuPostViews(){   
  26.     //注册小工具   
  27.     register_widget('AshuPostViews');   
  28. }   
  29. //widges_init,小工具初始化的时候执行AshuPostViews函数,   
  30. add_action('widgets_init','AshuPostViews');   
  31. ?>  

上面代码中,三个基本函数 form、update、widget函数名是固定的,小工具一般都要有着三个函数,当然小工具还能添加一下自定义的辅助函数。

一、类的构建函数

  1. function AshuPostViews(){   
  2.     $widget_ops = array('classname'=>'widget_postviews','description'=>'按浏览量显示文章');   
  3.     $this->WP_Widget(false,'热门浏览',$widget_ops);   
  4. }  

主要就是配置小工具的基本显示参数,WP_Widget函数的参数还能添加其它的。这里先不做介绍。

二、后台表单函数form()

这个函数也很简单,输出一个表单,谁不会呢?

  1. //参数$instance为之前保存过的数据   
  2. function form($instance){   
  3.     //如果之前没有数据的话,设置两个默认量   
  4.     $instance = wp_parse_args((array)$instance,array(   
  5.     'title'=>'热评文章','showPosts'=>10   
  6.     ));   
  7.            
  8.     $title = htmlspecialchars($instance['title']);   
  9.     $showPosts = htmlspecialchars($instance['showPosts']);   
  10.        
  11.     //表格布局的输出表单   
  12.     $output = '<table>';   
  13.     $output .= '<tr><td>标题:</td><td>';   
  14.     $output .= '<input id="'.$this->get_field_id('title') .'" name="'.$this->get_field_name('title').'" type="text" value="'.$instance['title'].'" />';   
  15.     $output .= '</td></tr><tr><td>文章数量:</td><td>';   
  16.     $output .= '<input id="'.$this->get_field_id('showPosts') .'" name="'.$this->get_field_name('showPosts').'" type="text" value="'.$instance['showPosts'].'" />';   
  17.     $output .= '</td></tr></table>';   
  18.     echo $output;   
  19. }  

只需要处理一下传入参数,其它的毫无压力。。图森破。
三、数据更新函数update()
这个函数也很简单,就是根据传入函数处理一下数据。

  1. //参数$new_instance是提交的数据   
  2. function update($new_instance,$old_instance){   
  3.     $instance = $old_instance;   
  4.     //数据处理   
  5.     $instance['title'] = strip_tags(stripslashes($new_instance['title']));   
  6.     $instance['showPosts'] = strip_tags(stripslashes($new_instance['showPosts']));   
  7.     //返回   
  8.     return $instance;   
  9. }  

不解释了。。
四、前台显示函数widget()

  1. //$args是注册侧边栏的注册的几个变量   
  2. //$instance是小工具的设置数据   
  3. function widget($args,$instance){   
  4.            
  5.     extract($args); //将数组展开   
  6.     $title = apply_filters('widget_title',emptyempty($instance['title']) ? '&nbsp;' : $instance['title']);   
  7.     $showPosts = emptyempty($instance['showPosts']) ? 10 : $instance['showPosts'];   
  8.        
  9.     echo $before_widget;   
  10.     echo $before_title . $title . $after_title;   
  11.     echo '<ul>';   
  12.         $this->ashu_get_hotpost($showPosts); //使用ashu_get_hotpost函数获取文章   
  13.     echo '</ul>';   
  14.     echo $after_widget;   
  15. }  

这里的几个变量$before_widget等是在我们注册侧边栏的时候顶下的,都保存在数组$args中,使用extract函数可将数组展开为几个变量。回顾一下注册侧边栏的时候:

  1. register_sidebar(array(   
  2.     'name'=>'首页侧边栏',   
  3.     'id'=>'sidebar-1',   
  4.     'before_widget' => '<div id="%1$s" class="widget %2$s">',   
  5.     'after_widget' => '</div>',   
  6.     'before_title' => '<h3>',   
  7.     'after_title' => '</h3>',   
  8.   ));  

有了这几个基本函数,即可制作一个小工具了,我们还少了一个函数,在widget函数中我调用了一个函数ashu_get_hotpost来获取文章,但是这个文章没有定义,所以我们得添加上这个函数,不然就出错了。

五:获取文章函数

  1. //$showposts参数为显示文章的数量   
  2. function ashu_get_hotpost($showposts){   
  3.     global $wpdb;   
  4.            
  5.     //使用自定义sql查询语句,注意,我的文章浏览量是保存在自定义字段post_views_count中   
  6.     $result = $wpdb->get_results("SELECT post_id,meta_key,meta_value,ID,post_title FROM $wpdb->postmeta key1 INNER JOIN $wpdb->posts key2 ON key1.post_id = key2.ID where key2.post_type='post' AND key2.post_status='publish' AND key1.meta_key='post_views_count' ORDER BY CAST(key1.meta_value AS SIGNED) DESC LIMIT 0 , $showposts");   
  7.            
  8.     $output = '';   
  9.   
  10.     foreach ($result as $post) {   
  11.         setup_postdata($post);   
  12.         $postid = $post->ID;   
  13.         //计算标题长度   
  14.         if( mb_strlen($post->post_title,"UTF-8")>18 ){   
  15.             $title = strip_tags($post->post_title);   
  16.             $short_title = trim(mb_substr($title ,0,14,"UTF-8"));   
  17.             $short_title .= '...';   
  18.         }else{   
  19.             $short_title = $post->post_title;   
  20.         }   
  21.   
  22.             $output .= '<li class=""><a href="' . get_permalink($postid) . '" title="' . $title .'">' . $short_title .'</a><br /> <span class="">'.$post->meta_value .'Views</span></li>';   
  23.     }     
  24.     echo $output;     
  25. }  

一个完整的按浏览量显示的小工具代码就只做完成了。。

但是别忘了还有最后一步,,,加载这个小工具文件

在functions.php中加入代码包含这个文件即可,比如我需要加载widget/hotposts.php:

  1. include_once(TEMPLATEPATH .'/widget/hotposts.php');  

完整代码

  1. <?php   
  2. /*
  3. Plugin Name:热门浏览  
  4. Plugin URI:http://www.kutailang.com  
  5. Description:热门浏览  
  6. Version:1.0  
  7. Author:树是我的朋友  
  8. Author URI:http://www.kutailang.com  
  9. */  
  10. //类   
  11. class AshuPostViews extends WP_Widget{   
  12.     function AshuPostViews(){   
  13.         $widget_ops = array('classname'=>'widget_postviews','description'=>'按浏览量显示文章');   
  14.         $this->WP_Widget(false,'热门浏览',$widget_ops);   
  15.     }   
  16.     //表单   
  17.     function form($instance){   
  18.         $instance = wp_parse_args((array)$instance,array(   
  19.         'title'=>'热评文章','showPosts'=>10   
  20.         ));   
  21.         $title = htmlspecialchars($instance['title']);   
  22.         $showPosts = htmlspecialchars($instance['showPosts']);   
  23.         $output = '<table>';   
  24.         $output .= '<tr><td>标题:</td><td>';   
  25.         $output .= '<input id="'.$this->get_field_id('title') .'" name="'.$this->get_field_name('title').'" type="text" value="'.$instance['title'].'" />';   
  26.         $output .= '</td></tr><tr><td>文章数量:</td><td>';   
  27.         $output .= '<input id="'.$this->get_field_id('showPosts') .'" name="'.$this->get_field_name('showPosts').'" type="text" value="'.$instance['showPosts'].'" />';   
  28.         $output .= '</td></tr></table>';   
  29.         echo $output;   
  30.     }   
  31.        
  32.     function update($new_instance,$old_instance){   
  33.         $instance = $old_instance;   
  34.         $instance['title'] = strip_tags(stripslashes($new_instance['title']));   
  35.         $instance['showPosts'] = strip_tags(stripslashes($new_instance['showPosts']));   
  36.         return $instance;   
  37.     }   
  38.        
  39.     function widget($args,$instance){   
  40.         extract($args);   
  41.         $title = apply_filters('widget_title',emptyempty($instance['title']) ? '&nbsp;' : $instance['title']);   
  42.         $showPosts = emptyempty($instance['showPosts']) ? 10 : $instance['showPosts'];   
  43.         echo $before_widget;   
  44.         echo $before_title . $title . $after_title;   
  45.         echo '<ul>';   
  46.             $this->ashu_get_hotpost($showPosts);   
  47.         echo '</ul>';   
  48.         echo $after_widget;   
  49.     }   
  50.     function ashu_get_hotpost($showposts){   
  51.     global $wpdb;      
  52.     $result = $wpdb->get_results("SELECT post_id,meta_key,meta_value,ID,post_title FROM $wpdb->postmeta key1 INNER JOIN $wpdb->posts key2 ON key1.post_id = key2.ID where key2.post_type='post' AND key2.post_status='publish' AND key1.meta_key='post_views_count' ORDER BY CAST(key1.meta_value AS SIGNED) DESC LIMIT 0 , $showposts");   
  53.     $output = '';   
  54.     foreach ($result as $post) {   
  55.   
  56.         $postid = $post->ID;      
  57.         if( mb_strlen($post->post_title,"UTF-8")>18 ){   
  58.             $title = strip_tags($post->post_title);   
  59.             $short_title = trim(mb_substr($title ,0,14,"UTF-8"));   
  60.             $short_title .= '...';   
  61.         }else{   
  62.             $short_title = $post->post_title;   
  63.         }   
  64.   
  65.             $output .= '<li class=""><a href="' . get_permalink($postid) . '" title="' . $title .'">' . $short_title .'</a><br /> <span class="">'.$post->meta_value .'Views</span></li>';   
  66.         $i++;   
  67.     }      
  68.     echo $output;      
  69.     }     
  70. }   
  71.   
  72. function AshuPostViews(){   
  73.     register_widget('AshuPostViews');   
  74. }   
  75. add_action('widgets_init','AshuPostViews');   
  76. ?>  

已有7条评论

aben进行回复 取消回复