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分类的永世链接是这样的比方本站的技术文章分类。 exehack.net/category/my-article/...
style.css : CSS(款式表)文件,普通包括主题申明和通用css款式代码 index.php : 主页模板,普通用来做网...
WordPress 的 Media 库治理器只能从客户端抉择文件上传,Dion Hulse 开发了一个 Add From Server 插件,支...
老鹰主机,是咱们站长应用较多的美国主机商之一。有些时分,咱们可能习气国际的一些主机商和管制面板的建站...
本文实例讲述了WordPress主动给文章增加nofollow属性的完成办法。分享给大家供大家参考。详细剖析如下: no...