本人做wordpress评论插件修正评论款式(两步丑化评论内容)

wordpress自带的近期评论小工具不会显示详细的评论内容,而且还会显示治理员的评论,觉得不是很好,只能本人解决一下。花了近一个早晨才解决好,次要用无理解小工具的原理上了,然而应用起来就十分简略了,只需简略的两个步骤。该小工具在wordpress 3.4.1版本上测试经过。先来个截图预览下:

1、制造小工具

代码一堆能够不必管它,间接将代码保留到wordpress的/wp-content/widgets/comments.php文件。

为什么放到这里呢?由于像主题、插件这些都是存在wp-content这个目录上面,小工具寄存在这里能够对立治理,也合乎wordpress目录规定。


<?php</p>
<p>/**
* 承继WP_Widget_Recent_Comments
* 这样就只要要重写widget办法就能够了
*/
class My_Widget_Recent_Comments extends WP_Widget_Recent_Comments {</p>
<p> /**
* 结构办法,次要是定义小工具的称号,引见
*/
function My_Widget_Recent_Comments() {
$widget_ops = array('classname' => 'my_widget_recent_comments', 'description' => __('显示**评论内容'));
$this->WP_Widget('my-recent-comments', __('我的**评论', 'my'), $widget_ops);
}</p>
<p> /**
* 小工具的渲染办法,这里就是输入评论
*/
function widget($args, $instance) {
global $wpdb, $comments, $comment;</p>
<p> $cache = wp_cache_get('my_widget_recent_comments', 'widget');</p>
<p> if (!is_array($cache))
$cache = array();</p>
<p> if (!isset($args['widget_id']))
$args['widget_id'] = $this->id;</p>
<p> if (isset($cache[$args['widget_id']])) {
echo $cache[$args['widget_id']];
return;
}</p>
<p> 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;</p>
<p> $output .= '<ul id="myrecentcomments">';
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);</p>
<p> 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), 0, '65', '...', 'UTF-8');
$content = convert_smilies($content);
//评论的文章
$post = '<a href="' . esc_url(get_comment_link($comment->comment_ID)) . '">' . get_the_title($comment->comment_post_ID) . '</a>';</p>
<p> //这里就是输入的html,能够依据需求自行修正
$output .= '<li class="comment" style="padding-bottom: 5px; ">
<p>
<table class="tablayout"><tbody><tr>
<td class="tdleft" style="width:55px;vertical-align:top;">' . $avatar . '</td>
<td class="tdleft" style="vertical-align:top;">
<p class="comment-author"><strong><span class="fn">' . $author . '</span></strong> <span class="says">宣布在 ' . $post . '</span></p>
</tr></tbody></table>
</p>
<p class="comment-content"><p class="last">' . $content . '</p>
</p>
</li>';
}
}
$output .= '</ul>';
$output .= $after_widget;</p>
<p> echo $output;
$cache[$args['widget_id']] = $output;
wp_cache_set('my_widget_recent_comments', $cache, 'widget');
}</p>
<p>}</p>
<p>//注册小工具
register_widget('My_Widget_Recent_Comments');

在主标题录下的funtions.php文件中加载这个小工具:


require(get_template_directory().'/../../widgets/comments.php');

2、进入后盾治理拖入评论小工具

抉择外观->小工具,能够看到评论小工具就加载出去了,如下:



间接将评论小工具拖动到侧边栏就OK了。

小结

有的冤家可能会担忧代码呈现什么成绩,这个代码是从零碎自带的评论小工具中复制过去的,次要是修正评论的获取和评论的显示款式。零碎自带的评论小工具代码能够参考/wp-includes/default-widgets.php中的WP_Widget_Recent_Comments类。

以上就是安达网络工作室关于《自己做wordpress评论插件修改评论样式(两步美化评论内容)》的一些看法。更多内容请查看本栏目更多内容!

版权声明:本文为 安达网络工作室 转载文章,如有侵权请联系我们及时删除。
相关文章
WordPress修正新用户注册邮件内容的办法

本文实例讲述了如何修正WordPress新用户注册邮件内容,由于零碎发送的邮件是纯文本类型的,页面不太美观,又没...

Wordpress 遗记明码的解决办法

1. Wordpress的明码在存入数据库是以md5码的方式寄存 依据此原理能够手动update数据库,从新设置自定的明码 ...

应用jQuery完成WordPress中的Ctrl+Enter和@评论回复

增加 Ctrl+Enter 快捷回复 这里送上两种办法,首先是 jQuery 办法,前段工夫不断在钻研 jQuery ,因而对 jQ...

查看本人或他人的WordPress版本默许为wp-login.php

明天在修正网站的时分,不小心把最新版Wordpress3.5.1的wp-includes目录下的functions.php笼罩出来了,招致...

wordpress动态化首页及去除url中的index.html

应用wordpass博客顺序的冤家都晓得他的默许首页是index.php,但是地球人都晓得index.html的太首页可以进步网...

wordpress去掉自带的logo或许左侧栏的菜双方法

去掉logo或许左侧栏的菜单,避免被改掉。 在应用模板下的functions.php下修正 复制代码代码如下: function ...

需求提交

客服服务