我想覆盖核心的最近评论小部件的输出。我扩展了WP_Widget_Recent_Comments
进行了所需的更改,它准确地显示了我想要的内容。然而,我现在已经用Monster小部件测试了主题,对于最近的评论小部件应该输出的位置,抛出了一个错误:
Notice: Undefined index: WP_Widget_Recent_Comments in /Users/tfisher/Sites/mysite/dev/wp-includes/widgets.php on line 1319
无法理解为什么它只有在被Monster小部件使用时才会抛出错误。
这是我的扩展类:
Class My_Recent_Comments_Widget extends WP_Widget_Recent_Comments {
function widget( $args, $instance ) {
global $comments, $comment;
$cache = wp_cache_get(\'widget_recent_comments\', \'widget\');
if ( ! is_array( $cache ) )
$cache = array();
if ( ! isset( $args[\'widget_id\'] ) )
$args[\'widget_id\'] = $this->id;
if ( isset( $cache[ $args[\'widget_id\'] ] ) ) {
echo $cache[ $args[\'widget_id\'] ];
return;
}
extract($args, EXTR_SKIP);
$output = \'\';
$title = ( ! empty( $instance[\'title\'] ) && isset($instance[\'title\']) ) ? $instance[\'title\'] : __( \'Recent Comments\', \'mysite\' );
/** This filter is documented in wp-includes/default-widgets.php */
$title = apply_filters( \'widget_title\', $title, $instance, $this->id_base );
$number = ( ! empty( $instance[\'number\'] ) && isset($instance[\'number\']) ) ? absint( $instance[\'number\'] ) : 5;
if ( ! $number )
$number = 5;
/**
* Filter the arguments for the Recent Comments widget.
*
* @since 3.4.0
*
* @see get_comments()
*
* @param array $comment_args An array of arguments used to retrieve the recent comments.
*/
$comments = get_comments( apply_filters( \'widget_comments_args\', array(
\'number\' => $number,
\'status\' => \'approve\',
\'post_status\' => \'publish\'
) ) );
$output .= $before_widget;
if ( $title )
$output .= $before_title . $title . $after_title;
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 );
foreach ( (array) $comments as $comment) {
$output .= \'<div class="recentcomments item clearfix">\';
if(get_avatar($comment)) {
$output .= \'<figure class="pull-left">\';
$output .= \'<a href="\' . get_comment_author_url() . \'">\';
$output .= get_avatar( $comment, 100 );
$output .= \'</a>\';
$output .= \'</figure>\';
$output .= \'<div class="content">\';
} else {
$output .= \'<div class="content no-img">\';
}
$output .= sprintf(_x(\'%1$s on %2$s\', \'widgets\'), get_comment_author_link(), \'<a href="\' . esc_url( get_comment_link($comment->comment_ID) )) . \'">\';
$output .= get_the_title($comment->comment_post_ID) . \'</a>\';
$output .= \'</div>\'; // content
$output .= \'</div>\'; // item
}
}
$output .= $after_widget;
echo $output;
$cache[$args[\'widget_id\']] = $output;
wp_cache_set(\'widget_recent_comments\', $cache, \'widget\');
}
}