之前介绍了如何让 WordPress 主题支持小工具的方法,默认的情况,只有不几个小工具,而且在国内这些小工具好多都不太常用。
如果你想给你的主题添加一个自定义小工具,或者你要开发一个添加小工具的插件,那就需要查看本文了。
创建小工具
创建一个小工具,需要使用 register_widget() 函数挂载一个继承于 WP_Widget 类的类,下边是一个简单的例子,创建了一个随机文章小工具。
注意,register_widget() 函数需要在 widgets_init 钩子上调用。
class widget_tags extends WP_Widget{ //添加小工具 function __construct(){ $this->WP_Widget( 'random_posts', __( '随机文章', 'Bing' ), array( 'description' => __( '显示几篇随机文章', 'Bing' ) ) ); } //小工具内容 function widget( $args, $instance ){ //导入当前侧边栏设置 extract( $args, EXTR_SKIP ); //输出小工具前代码 echo $before_widget; //输出小工具标题 echo $before_title . '随机文章' . $after_title; //随机文章 query_posts( 'orderby=rand&showposts=10' ); if( have_posts() ): echo '<ul>'; while( have_posts() ): the_post(); printf( '<li><a href="%s" title="%s">%s</a></li>', get_permalink(), get_the_title(), get_the_title() ); endwhile; echo '</ul>'; endif; //输出小工具后代码 echo $after_widget; } } function Bing_add_widget_tags(){ register_widget( 'widget_tags' ); } add_action( 'widgets_init', 'Bing_add_widget_tags' );
这样,后台就会出现了一个随机文章小工具,拖动到侧边栏上,会在前台显示十篇随机文章。
小工具设置
但我们会发现这个小工具并没有设置选项,那如何给这个小工具添加设置选项呢?设置选项涉及类的两个函数,update()(更新小工具设置时的处理函数)和 form()(设置表单)。
下边的代码添加了一个标题设置和显示文章数量的设置:
class widget_tags extends WP_Widget{ //添加小工具 function __construct(){ $this->WP_Widget( 'random_posts', __( '随机文章', 'Bing' ), array( 'description' => __( '显示几篇随机文章', 'Bing' ) ) ); } //小工具内容 function widget( $args, $instance ){ //导入当前侧边栏设置 extract( $args, EXTR_SKIP ); //输出小工具前代码 echo $before_widget; //输出小工具标题 echo $before_title . '随机文章' . $after_title; //随机文章 query_posts( 'orderby=rand&showposts=10' ); if( have_posts() ): echo '<ul>'; while( have_posts() ): the_post(); printf( '<li><a href="%s" title="%s">%s</a></li>', get_permalink(), get_the_title(), get_the_title() ); endwhile; echo '</ul>'; endif; //输出小工具后代码 echo $after_widget; } //更新选项 function update( $instance, $old_instance ){ $instance['title'] = strip_tags( $instance['title'] ); $instance['number'] = (int) strip_tags( $instance['number'] ); return $instance; } //选项表单 function form( $instance ){ //添加默认设置 $instance = wp_parse_args( $instance, array( 'title' => __( '随机文章', 'Bing' ), 'number' => 10 ) ); //设置表单 ?> <p> <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( '标题', 'Bing' ); ?>:</label> <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo $instance['title']; ?>" /> </p> <p> <label for="<?php echo $this->get_field_id( 'number' ); ?>"><?php _e( '文章数量', 'Bing' ); ?>:</label> <input class="widefat" id="<?php echo $this->get_field_id( 'number' ); ?>" name="<?php echo $this->get_field_name( 'number' ); ?>" type="number" value="<?php echo $instance['number']; ?>" /> </p> <?php } } function Bing_add_widget_tags(){ register_widget( 'widget_tags' ); } add_action( 'widgets_init', 'Bing_add_widget_tags' );
这样再展开小工具,就能看到设置了:
然后,在生成小工具内容的时候使用选项,就能达到用户自定义的效果。
class widget_tags extends WP_Widget{ //添加小工具 function __construct(){ $this->WP_Widget( 'random_posts', __( '随机文章', 'Bing' ), array( 'description' => __( '显示几篇随机文章', 'Bing' ) ) ); } //小工具内容 function widget( $args, $instance ){ //导入当前侧边栏设置 extract( $args, EXTR_SKIP ); //添加小工具标题过滤器 $title = apply_filters( 'widget_name', $instance['title'] ); //输出小工具前代码 echo $before_widget; //输出小工具标题 echo $before_title . $title . $after_title; //随机文章 query_posts( 'orderby=rand&showposts=' . $instance['number'] ); if( have_posts() ): echo '<ul>'; while( have_posts() ): the_post(); printf( '<li><a href="%s" title="%s">%s</a></li>', get_permalink(), get_the_title(), get_the_title() ); endwhile; echo '</ul>'; endif; //输出小工具后代码 echo $after_widget; } //更新选项 function update( $instance, $old_instance ){ $instance['title'] = strip_tags( $instance['title'] ); $instance['number'] = (int) strip_tags( $instance['number'] ); return $instance; } //选项表单 function form( $instance ){ //添加默认设置 $instance = wp_parse_args( $instance, array( 'title' => __( '随机文章', 'Bing' ), 'number' => 10 ) ); //设置表单 ?> <p> <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( '标题', 'Bing' ); ?>:</label> <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo $instance['title']; ?>" /> </p> <p> <label for="<?php echo $this->get_field_id( 'number' ); ?>"><?php _e( '文章数量', 'Bing' ); ?>:</label> <input class="widefat" id="<?php echo $this->get_field_id( 'number' ); ?>" name="<?php echo $this->get_field_name( 'number' ); ?>" type="number" value="<?php echo $instance['number']; ?>" /> </p> <?php } } function Bing_add_widget_tags(){ register_widget( 'widget_tags' ); } add_action( 'widgets_init', 'Bing_add_widget_tags' );
原文地址:http://www.cnblogs.com/xiaobingch/p/16927536.html
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,请务用于商业用途!
3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员!
8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载
声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性