加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > CMS系统 > wordpress > 正文

自己做wordpress评论插件修改评论样式(两步美化评论内容)

发布时间:2020-12-14 14:48:02 所属栏目:wordpress 来源:网络整理
导读:wordpress自带的近期评论小工具不会显示具体的评论内容,而且还会显示管理员的评论,感觉不是很好,只能自己处理一下。花了近一个晚上才处理好,主要用在理解小工具的原理上了,但是使用起来就非常简单了,只要简单的两个步骤。该小工具在wordpress 3.4.1版

wordpress自带的近期评论小工具不会显示具体的评论内容,而且还会显示管理员的评论,感觉不是很好,只能自己处理一下。花了近一个晚上才处理好,主要用在理解小工具的原理上了,但是使用起来就非常简单了,只要简单的两个步骤。该小工具在wordpress 3.4.1版本上测试通过。先来个截图预览下:

1、制作小工具

代码一堆可以不用管它,直接将代码保存到wordpress的/wp-content/widgets/comments.php文件。

为什么放到这里呢?因为像主题、插件这些都是存在wp-content这个目录下面,小工具存放在这里可以统一管理,也符合wordpress目录规则。

/** * 继承WP_Widget_Recent_Comments * 这样就只需要重写widget方法就可以了 */class My_Widget_Recent_Comments extends WP_Widget_Recent_Comments {

/** * 构造方法,主要是定义小工具的名称,介绍 */ function My_Widget_Recent_Comments() { $widget_ops = array('classname' => 'my_widget_recent_comments','description' => __('显示最新评论内容')); $this->WP_Widget('my-recent-comments',__('我的最新评论','my'),$widget_ops); }

/** * 小工具的渲染方法,这里就是输出评论 */ function widget($args,$instance) { global $wpdb,$comments,$comment;

$cache = wp_cache_get('my_widget_recent_comments','widget');

if (!is_array($cache)) $cache = array();

if (!isset($args['widget_id'])) $args['widget_id'] = $this->id;

if (isset($cache[$args['widget_id']])) { echo $cache[$args['widget_id']]; return; }

extract($args,EXTR_SKIP); $output = ''; $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;

$output .= '

    foreach ((array) $comments as $comment) { //头像 $avatar = get_avatar($comment,40); //作者名称 $author = get_comment_author(); //评论内容 $content = apply_filters('get_comment_text',$comment->comment_content); $content = mb_strimwidth(strip_tags($content),'65','...','UTF-8'); $content = convert_smilies($content); //评论的文章 $post = 'comment_ID)) . '">' . get_the_title($comment->comment_post_ID) . '';

    //这里就是输出的html,可以根据需要自行修改 $output .= '

  • '; } } $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');

    (编辑:李大同)

    【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!