起因:让WordPress的最近评论Widget直接显示评论内容,而那个Get Recent Comments插件功能太多,太庞大。我用不着,看着好好的wp默认的widget浪费掉于心不忍,又不想改源代码,懒得写插件。
总结:要简单而强大!(其实就是追求完美的懒人。)
经过:看代码,发现
function wp_widget_recent_comments_register() { $widget_ops = array('classname' => 'widget_recent_comments', 'description' => __( 'The most recent comments' ) ); wp_register_sidebar_widget('recent-comments', __('Recent Comments'), 'wp_widget_recent_comments', $widget_ops); wp_register_widget_control('recent-comments', __('Recent Comments'), 'wp_widget_recent_comments_control'); if ( is_active_widget('wp_widget_recent_comments') ) add_action('wp_head', 'wp_widget_recent_comments_style'); }
我相信——既然有register就相信一定有untegister
Google后找到WordPress Development: Replacing Default Widgets
在functions.php里添上Leo的代码:
function yixia_widget_recent_comments($args) { global $wpdb, $comments, $comment; extract($args, EXTR_SKIP); $options = get_option('widget_recent_comments'); $title = empty($options['title']) ? __('Recent Comments') : $options['title']; if ( !$number = (int) $options['number'] ) $number = 5; else if ( $number < 1 ) $number = 1; else if ( $number > 15 ) $number = 15; if ( !$comments = wp_cache_get( 'recent_comments', 'widget' ) ) { $comments = $wpdb->get_results("SELECT comment_author, comment_author_url, comment_ID, comment_post_ID,comment_content FROM $wpdb->comments WHERE comment_approved = '1' ORDER BY comment_date_gmt DESC LIMIT $number"); wp_cache_add( 'recent_comments', $comments, 'widget' ); } ?> <?php echo $before_widget; ?> <?php echo $before_title . $title . $after_title; ?> <ul id="recentcomments"><?php if ( $comments ) : foreach ($comments as $comment) : $comment_content = strip_tags($comment->comment_content); $comment_content = stripslashes($comment_content); // $comment_content = preg_replace('/\[qu(.(?!\[\/quote]))+.\[\/quote]/si', '', $comment_content); // $comment_content = preg_replace('/\s*:em\d\d:\s*/si', '', $comment_content); $comment_excerpt =substr($comment_content,0,50); $comment_excerpt = my_utf8_trim($comment_excerpt); echo '<li class="recentcomments">' . sprintf(__('%1$s: %2$s'), get_comment_author_link(), '<a href="'. get_permalink($comment->comment_post_ID) . '#comment-' . $comment->comment_ID . '" title="'. get_the_title($comment->comment_post_ID) .'">' . $comment_excerpt .'...'. '</a>') . '</li>'; endforeach; endif;?></ul> <?php echo $after_widget; ?> <?php } function my_utf8_trim($str) { $len = strlen($str); for ($i=strlen($str)-1; $i>=0; $i-=1) { $hex .= ' '.ord($str[$i]); $ch = ord($str[$i]); if (($ch & 128)==0) return(substr($str,0,$i)); if (($ch & 192)==192) return(substr($str,0,$i)); } return($str.$hex); }
最后注册自己的yixia_widget_recent_comments:
function yixia_widget_register() { // unregister the widget and its control wp_unregister_sidebar_widget('recent-comments'); // register the new and improved widget and control $widget_ops = array('classname' => 'widget_recent_comments', 'description' => __( 'The most recent comments' ) ); wp_register_sidebar_widget('recent-comments', __('Recent Comments'), 'yixia_widget_recent_comments', $widget_ops); wp_register_widget_control('recent-comments', __('Recent Comments'), 'wp_widget_recent_comments_control'); } add_action('widgets_init', 'yixia_widget_register', 1);
收工,睡觉。
呵呵,帮你PP顶!也请来vpn看看……
好厉害哦,楼主可以写个详细教程吗?