折腾侧边栏最新评论
| 折腾 | 12,499 views | 27 replies

今天很高产啊,这是今天第三篇了!这篇就是专门来折腾侧边栏最近评论的。可以说完全是大发的功劳,我是直接扒了他的Presscore Lite主题里的“最新评论”小工具!扒这个工具比折腾functions.php文件容易多了,因为这些都是现成的!
直接介绍方法,这是在未经大发允许的情况下发,所以版权归大发所有!
首先找到你主题的widget.php文件,有可能会是其他的,但是基本上都是这个文件,然后在适当的位置加上:
class bigfa_widget3 extends WP_Widget {
function bigfa_widget3() {
$widget_ops = array('description' => '支持评论表情');
$this->WP_Widget('bigfa_widget3', 'P-主题最新评论', $widget_ops);
}
function widget($args, $instance) {
extract($args);
$title = apply_filters('widget_title',esc_attr($instance['title']));
$limit = strip_tags($instance['limit']);
$email = strip_tags($instance['email']);
echo $before_widget.$before_title.$title.$after_title;
?>
<ul class="recentcomments">
<?php
global $wpdb;
$limit_num = $limit ;
$my_email ="'" . $email . "'";
$rc_comms = $wpdb->get_results("SELECT ID, post_title, comment_ID, comment_author,comment_author_email,comment_date,comment_content FROM $wpdb->comments LEFT OUTER JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID = $wpdb->posts.ID) WHERE comment_approved = '1' AND comment_type = '' AND post_password = '' AND comment_author_email != $my_email ORDER BY comment_date_gmt DESC LIMIT $limit_num ");
$rc_comments = '';foreach ($rc_comms as $rc_comm) { $rc_comments .= "<li>" . get_avatar($rc_comm,$size='30') ."<div class='rc-info'><span class='rc-reviewer'>".$rc_comm->comment_author."</span>在<a class='rc-post' href='". get_permalink($rc_comm->ID) . "#comment-" . $rc_comm->comment_ID. "'>" . $rc_comm->post_title . "</a></div><div class='rc_excerpt'>".$rc_comm->comment_content."</div></li>\n";}
$rc_comments = convert_smilies($rc_comments);
echo $rc_comments;
?>
</ul>
<?php
echo $after_widget;
}
function update($new_instance, $old_instance) {
if (!isset($new_instance['submit'])) {
return false;
}
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
$instance['limit'] = strip_tags($new_instance['limit']);
$instance['email'] = strip_tags($new_instance['email']);
return $instance;
}
function form($instance) {
global $wpdb;
$instance = wp_parse_args((array) $instance, array('title'=> '', 'limit' => '', 'email' => ''));
$title = esc_attr($instance['title']);
$limit = strip_tags($instance['limit']);
$email = strip_tags($instance['email']);
?>
<p>
<label for="<?php echo $this->get_field_id('title'); ?>">标题:<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /></label>
</p>
<p>
<label for="<?php echo $this->get_field_id('limit'); ?>">显示数量:(最好5个以下) <input class="widefat" id="<?php echo $this->get_field_id('limit'); ?>" name="<?php echo $this->get_field_name('limit'); ?>" type="text" value="<?php echo $limit; ?>" /></label>
</p>
<p>
<label for="<?php echo $this->get_field_id('email'); ?>">输入你的邮箱以排除显示你的回复: <br>(留空则不排除)<input class="widefat" id="<?php echo $this->get_field_id('email'); ?>" name="<?php echo $this->get_field_name('email'); ?>" type="text" value="<?php echo $email; ?>" /></label>
</p>
<input type="hidden" id="<?php echo $this->get_field_id('submit'); ?>" name="<?php echo $this->get_field_name('submit'); ?>" value="1" />
<?php
}
}
add_action('widgets_init', 'bigfa_widget3_init');
function bigfa_widget3_init() {
register_widget('bigfa_widget3');
}
function bigfa_widget3() {
$widget_ops = array('description' => '支持评论表情');
$this->WP_Widget('bigfa_widget3', 'P-主题最新评论', $widget_ops);
}
function widget($args, $instance) {
extract($args);
$title = apply_filters('widget_title',esc_attr($instance['title']));
$limit = strip_tags($instance['limit']);
$email = strip_tags($instance['email']);
echo $before_widget.$before_title.$title.$after_title;
?>
<ul class="recentcomments">
<?php
global $wpdb;
$limit_num = $limit ;
$my_email ="'" . $email . "'";
$rc_comms = $wpdb->get_results("SELECT ID, post_title, comment_ID, comment_author,comment_author_email,comment_date,comment_content FROM $wpdb->comments LEFT OUTER JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID = $wpdb->posts.ID) WHERE comment_approved = '1' AND comment_type = '' AND post_password = '' AND comment_author_email != $my_email ORDER BY comment_date_gmt DESC LIMIT $limit_num ");
$rc_comments = '';foreach ($rc_comms as $rc_comm) { $rc_comments .= "<li>" . get_avatar($rc_comm,$size='30') ."<div class='rc-info'><span class='rc-reviewer'>".$rc_comm->comment_author."</span>在<a class='rc-post' href='". get_permalink($rc_comm->ID) . "#comment-" . $rc_comm->comment_ID. "'>" . $rc_comm->post_title . "</a></div><div class='rc_excerpt'>".$rc_comm->comment_content."</div></li>\n";}
$rc_comments = convert_smilies($rc_comments);
echo $rc_comments;
?>
</ul>
<?php
echo $after_widget;
}
function update($new_instance, $old_instance) {
if (!isset($new_instance['submit'])) {
return false;
}
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
$instance['limit'] = strip_tags($new_instance['limit']);
$instance['email'] = strip_tags($new_instance['email']);
return $instance;
}
function form($instance) {
global $wpdb;
$instance = wp_parse_args((array) $instance, array('title'=> '', 'limit' => '', 'email' => ''));
$title = esc_attr($instance['title']);
$limit = strip_tags($instance['limit']);
$email = strip_tags($instance['email']);
?>
<p>
<label for="<?php echo $this->get_field_id('title'); ?>">标题:<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /></label>
</p>
<p>
<label for="<?php echo $this->get_field_id('limit'); ?>">显示数量:(最好5个以下) <input class="widefat" id="<?php echo $this->get_field_id('limit'); ?>" name="<?php echo $this->get_field_name('limit'); ?>" type="text" value="<?php echo $limit; ?>" /></label>
</p>
<p>
<label for="<?php echo $this->get_field_id('email'); ?>">输入你的邮箱以排除显示你的回复: <br>(留空则不排除)<input class="widefat" id="<?php echo $this->get_field_id('email'); ?>" name="<?php echo $this->get_field_name('email'); ?>" type="text" value="<?php echo $email; ?>" /></label>
</p>
<input type="hidden" id="<?php echo $this->get_field_id('submit'); ?>" name="<?php echo $this->get_field_name('submit'); ?>" value="1" />
<?php
}
}
add_action('widgets_init', 'bigfa_widget3_init');
function bigfa_widget3_init() {
register_widget('bigfa_widget3');
}
这样就大功告成了,直接在小工具找到,拖到侧边栏就OK啦!
这样还不行,需要添加样式CSS,在你style.css文件里适当位置加上:
.recentcomments{font-size:12px}
.recentcomments li{color:#555;padding-bottom:8px;margin-top:10px;border-bottom:1px dashed #DFDFDF}
.avatar-30{display:inline;float:left;margin-right:10px;margin-top:4px;width:42px;height:42px}
.rc-reviewer{font-weight:400;display:inline;color:#333!important}
.rc-post{color:#999;display:inline}
.rc-post:hover{color:#d14836}
.rc-info{}
.rc-excerpt{margin-left:40px;color:#666}
.rc-older,.rc-newer{margin-top:5px}
.rc-older:hover,.rc-newer:hover{color:#d14836}
.recentcomments li{color:#555;padding-bottom:8px;margin-top:10px;border-bottom:1px dashed #DFDFDF}
.avatar-30{display:inline;float:left;margin-right:10px;margin-top:4px;width:42px;height:42px}
.rc-reviewer{font-weight:400;display:inline;color:#333!important}
.rc-post{color:#999;display:inline}
.rc-post:hover{color:#d14836}
.rc-info{}
.rc-excerpt{margin-left:40px;color:#666}
.rc-older,.rc-newer{margin-top:5px}
.rc-older:hover,.rc-newer:hover{color:#d14836}
打完收工,上面有些细节包括CSS样式都可以自己调整。最终效果图如下:

百度一下,大一推。
额~我是折腾了一下,始终不能屏蔽自己的评论,才想到扒大发的!
屏蔽自己不就是加个判断吗,最好是用邮箱来判断。
你的代码用的啥玩意编辑的。
加判断什么的试了的,好像使用use_id来判断,但是失败!用Notepad++编辑的。话说为喵的我自己点的表情显示不出来,显示的是代码?
user_id判断失败,说明在写入数据库的时候没有加上这个,我用牧风的Comment Ajax就有这个问题,让我很郁闷。
表情没显示,去后台的Writing Settings中第一个设置点上。
转换文本表情为图片早就点上了……
将echo get_comment_text()换成comment_text()
在comments.php找,还是在functions.php找?
呃。。。一般在function.php中。
解决可以显示,但是@回复之后的评论自动断行了……
看下评论显示是不是用的comment_text这个函数。
是的。ps:你看你刚发的一串显示出来也是代码了。。
说实话,你还真难折腾,折腾来折腾去,还不是那个鸟样啊
折腾出来有成就感,哈哈
你这。。。够直接额
这个要比那个状态简单一点的。
去掉了一些我不想要的东东,呵呵
好高端!
哈哈,换主题了原来~这个主题有点像mufeng的
就是牧风的主题啊,稍微修改了下!
哦~~直接弄成了小工具啦~
是的!
找不到widget.php啊…
啊,我开窍了….
多折腾多摸索什么都会了,我就是这么过来的,我也还是菜鸟~