WordPress最新评论小工具带头像带气泡纯代码实现方法
大部分Wordpress主题都会启用小工具这个功能,话说这是个很傻瓜的功能,但相当实用,只需通过后台拖拽就能实现前台各种显示问题。
现在给大家提供一段制作小工具的代码,并奉上明凯博客自用的侧栏最新评论列表模块,希望对你有用。
下面是后台最新评论小工具的添加方法:
把下面代码放到functions.php中:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | // 最新评论带头像评论 add_action('widgets_init', create_function('', 'return register_widget("widget_newcomments");')); class widget_newcomments extends WP_Widget { function widget_newcomments() { $option = array('classname' => 'widget_newcomments', 'description' => '显示网友最新评论(头像+名称+评论)' ); $this->WP_Widget(false, '最新评论 ', $option); } function widget($args, $instance) { extract($args, EXTR_SKIP); echo $before_widget; $title = empty($instance['title']) ? '最新评论' : apply_filters('widget_title', $instance['title']); $count = empty($instance['count']) ? '5' : apply_filters('widget_count', $instance['count']); echo $before_title . $title . $after_title; echo '<ul class="newcomments">'; echo newcomments( $count ); echo '</ul>'; echo $after_widget; } function update($new_instance, $old_instance) { $instance = $old_instance; $instance['title'] = strip_tags($new_instance['title']); $instance['count'] = strip_tags($new_instance['count']); return $instance; } function form($instance) { $instance = wp_parse_args( (array) $instance, array( 'title' => '', 'count' => '' ) ); $title = strip_tags($instance['title']); $count = strip_tags($instance['count']); echo '<p><la bel>标题:<in put id="'.$this-></in>get_field_id('title').'" name="'.$this->get_field_name('title').'" type="text" value="'.attribute_escape($title).'" size="24" /></la></p>'; echo '<p><label>数目:<input id="'.$this-/>get_field_id('count').'" name="'.$this->get_field_name('count').'" type="text" value="'.attribute_escape($count).'" size="3" /></label></p>'; } } function newcomments( $limit ){ global $wpdb; $sql = "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 = '' ORDER BY comment_date_gmt DESC LIMIT $limit"; $comments = $wpdb->get_results($sql); foreach ( $comments as $comment ) { $output .= "< li>" . get_avatar($comment->comment_author_email,32) ."<div class='rcomment'><stron g>".$comment->comment_author." " .human_time_diff(strtotime($comment->comment_date), current_time('timestamp'))."前 在 <a href='". get_comment_link($comment->comment_ID) . "' title=' " . $comment->post_title . "'> " . $comment->post_title . "</a> 评论:<div class='box'><span class='jt'>◆<span class='jt2'>◆</span></span>". convert_smilies($comment->comment_content)."</div></stron></div>"; } echo $output; }; |
下面是最新评论的样式了:
1 2 3 4 | .newcomments div.rcomment{margin-left:50px;} .newcomments div.box{background-color:#EEEEEE;position:relative;border:1px solid #CCCCCC;padding: 5px;border-radius: 5px;} .newcomments div.box .jt{position:absolute;color:#CCCCCC;left:50px;top:-12px;font-size:22px;line-height: 24px;font-family:arial,verdana,sans-serif;} .newcomments div.box .jt2{position:absolute;color:#EEEEEE;top:1px;left:0;} |
是不是有一种很很美丽的感觉。
MySQL启动的error 2003和1067错误解决方案 WordPress不同页面设置不同侧边栏方法