电脑技术网——专业手机电脑知识平台,关注科技、手机、电脑、智能硬件
帝国CMS织梦CMSPhpCMSWordPressDiscuz!

最新版WordPress小工具制作

2019-11-22 16:05:22 出处:[ 菜菜电脑网 ] 人气:次阅读

本文实例一部了WordPress小工具制作方法。体会给大家外大家参看,具体情况如下:

 

WordPress是一个包括着无与伦比推广性的软件,它的侧边栏小工具很是简便。但是配置文件的那几个小工具只不过过于用,或者说样式显然真正只能满足需要。今天就概述一下如何制作一个小工具,然后接下来再得出结论一个评论小工具的制作实例。

 

小工具三个部分,后台推断、数据存放、前台表明。当然如果你的小工具不只能在后台设置什么数据,那数据遗留可以省掉了。一般来讲,一个小工具至少不应有这三个部分。

 

小工具是一个类,像侧边栏一样,你还不对代码持有人它,它在能在后台用作。



代码如下:

//定义小工具类PostViews

class PostViews extends WP_Widget{

 function PostViews(){

 //这是定义小工具信息的函数,也是类的重构函数

}

function form($instance){

 //这是表单函数,也就是操纵后台辨识的

}

function update($new_instance,$old_instance){

 //这是更新数据函数,小工具如果有设置选项,就只能存放更新数据

}

function widget($args,$instance){

 //这是支配小工具前台推断的函数

}

}

function PostViews(){

 //申请小工具

register_widget('PostViews');

}

//widges_init,小工具初始化的时候分派PostViews函数,

add_action('widgets_init','PostViews');


 

根据代码可真的,主要是传给WordPress的WP_Widget类,并且载重里面的函数,以此来远超自定义小工具的目的。

 

所附:近期评论工具制作

 

WordPress其实自带有一个近期评论的小工具,但是那个只有表明谁在哪篇文章上面评论了,非常可笑,无论如何很难受限制我们的所需。这次来解释的小工具可以推断用户头像,评论内容,已经时间等各方面适合于的信息。

 

还是和前面一样,继承人 WP_Widget_Recent_Comments 类,代码:




代码如下:

/**

 * 传给WP_Widget_Recent_Comments

 * 这样就只只能编写widget方法就可以了

 */

class My_Widget_Recent_Comments extends WP_Widget_Recent_Comments {

 /**

 * 构造方法,主要是定义小工具的名称,引介

*/

function My_Widget_Recent_Comments() {

 $widget_ops = array('classname' => 'widget_recent_comment', 'description' => __('推断最新评论内容'));

 $this->WP_Widget('my-recent-comments', __('我的最新评论', 'my'), $widget_ops);

 }

 /**

 * 小工具的三维方法,这里就是可用评论

*/

function widget($args, $instance) {

 global $wpdb, $comments, $comment;

 $title = apply_filters('widget_title', empty($instance['title']) ? __('Recent Comments') : $instance['title'], $instance, $this->id_base);

 if (empty($instance['number']) || !$number = absint($instance['number']))

 $number = 5;

 //借助评论,填充掉管理员自己

$comments = $wpdb->get_results("SELECT * FROM $wpdb->comments WHERE user_id !=2 and comment_approved = '1' and comment_type not in ('pingback','trackback') ORDER BY comment_date_gmt DESC LIMIT $number");

 $output .= $before_widget;

 if ($title)

 $output .= $before_title . $title . $after_title;

 if ($comments) {

 // Prime cache for associated posts. (Prime post term cache if we need it for permalinks.)

 $post_ids = array_unique(wp_list_pluck($comments, 'comment_post_ID'));

 _prime_post_caches($post_ids, strpos(get_option('permalink_structure'), '%category%'), false);

 foreach ((array) $comments as $comment) {

 //头像

$avatar = get_avatar($comment, 40);

 //作者名称

$author = get_comment_author();

 //评论内容

$content = apply_filters('get_comment_text', $comment->comment_content);

 $content = convert_smilies($content);

 //评论的文章

$post = '' . get_the_title($comment->comment_post_ID) . '';

 //这里就是转换成的html,可以根据必须自行更改

$output .= ''

 }

 }

 $output .= $after_widget;

 echo $output;

 $cache[$args['widget_id']] = $output;

 wp_cache_set('my_widget_recent_comments', $cache, 'widget');

 }

}


 

完了之后还要特许小工具,这样就可以在后台吊车了

 





激活代码

代码如下:

//申领小工具

register_widget('My_Widget_Recent_Comments');


 

决心本文所述对大家基于wordpress的程序设计有所尽力。


关于我们 - 广告合作 - 联系我们 - 免责声明 - 网站地图 - 投诉建议 - 在线投稿

©CopyRight 2008-2020 caicaipc.com Inc All Rights Reserved.
菜菜电脑网 版权所有