WP不仅提供了近期文章小工具,还给我们提供了近期评论小工具,功能是在网站侧边栏展示我们网站的最新评论,可以指定显示的评论条数。下面我们就来看看WordPress近期评论小工具的相关内容详解。
WordPress近期评论小工具使用介绍
在网站【后台-外观-小工具】中找到近期评论小工具,将它拖到想要展示的侧边栏中。
我们可以设置近期评论小工具的显示标题和显示的评论数量。
这样我们就很简单的在网站中加入了近期评论的功能了。
WordPress近期评论小工具源码介绍
源码位置:wp-includes\widgets\class-wp-widget-recent-comments.php
源代码:(since 4.4.0)
'widget_recent_comments',
'description' => __( 'Your site’s most recent comments.' ),
'customize_selective_refresh' => true,
);
parent::__construct( 'recent-comments', __( 'Recent Comments' ), $widget_ops );
$this->alt_option_name = 'widget_recent_comments';
if ( is_active_widget( false, false, $this->id_base ) || is_customize_preview() ) {
add_action( 'wp_head', array( $this, 'recent_comments_style' ) );
}
}
/**
* Outputs the default styles for the Recent Comments widget.
*
* @since 2.8.0
* @access public
*/
public function recent_comments_style() {
/**
* Filters the Recent Comments default widget styles.
*
* @since 3.1.0
*
* @param bool $active Whether the widget is active. Default true.
* @param string $id_base The widget ID.
*/
if ( ! current_theme_supports( 'widgets' ) // Temp hack #14876
|| ! apply_filters( 'show_recent_comments_widget_style', true, $this->id_base ) )
return;
?>
id;
$output = '';
$title = ( ! empty( $instance['title'] ) ) ? $instance['title'] : __( 'Recent Comments' );
/** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
$title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
$number = ( ! empty( $instance['number'] ) ) ? absint( $instance['number'] ) : 5;
if ( ! $number )
$number = 5;
/**
* Filters the arguments for the Recent Comments widget.
*
* @since 3.4.0
*
* @see WP_Comment_Query::query() for information on accepted arguments.
*
* @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 .= $args['before_widget'];
if ( $title ) {
$output .= $args['before_title'] . $title . $args['after_title'];
}
$output .= '
- ';
if ( is_array( $comments ) && $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 .= '
- '; /* translators: comments widget: 1: comment author, 2: post link */ $output .= sprintf( _x( '%1$s on %2$s', 'widgets' ), '' . get_the_title( $comment->comment_post_ID ) . '' ); $output .= ' '; } } $output .= ' ', '
WordPress近期评论小工具的介绍就到这里,后面我们会讲解如何自定义近期评论小工具。
感觉本站内容不错,读后有收获?小额赞助
还可以分享文章给好友: