首页 > 分类 > 网络百科 > 文章内容

WordPress 原生函数实现读者评论排行榜

weisaybox 主题里集成了读者评论排行榜功能,代码就跟露兜即刻这篇文章原理一样,主要使用了 SQL 查询语句来实现核心功能。为了后期维护方便,少接触 SQL 语句,也使得代码更“优雅”一些,我还是想找到使用 WordPress 原生 API 实现功能的版本。

不过没找到。

所以就花了一天时间把这个功能使用 WordPress 原生函数改造了一遍。使用了一段时间貌似还没发现什么问题,所以拿出来分享一下,抛砖引玉,或者共同来发现一些 bug。

贴代码:

function syshut_commenter_list( $number_commenters, $aftertime ) {
	if ( ! $output = get_option( 'syshut_commenter_list' ) ) {
		global $post;

		$exclude_ids = array();
		$myposts = get_posts(
			array(
				'has_password' => true,
			)
		);
		if ( $myposts ) {
			foreach ( $myposts as $post ) {
				setup_postdata( $post );
				$exclude_ids[] = get_the_ID();
			}
			wp_reset_postdata();
		}

		$comments = get_comments(
			array(
				'author__not_in' => get_the_author_meta( 'ID' ),
				'date_query'     => array(
					'after'  => $aftertime,
					'before' => 'tomorrow',
				),
				'post__not_in'   => $exclude_ids,
				'post_status'    => 'publish',
				'status'         => 'approve',
			)
		);

		$comment_email_group = array();
		foreach ( $comments as $comment ) {
			$comment_author_email = get_comment_author_email( $comment );

			if ( ! isset( $comment_email_group[ $comment_author_email ] ) ) {
				$comment_email_group[ $comment_author_email ] = array(
					'comment_author'     => get_comment_author( $comment ),
					'comment_author_url' => empty( $comment->comment_author_url ) ? '' : get_comment_author_url( $comment ),
					'comment_count'      => 1,
				);
			} else {
				$comment_email_group[ $comment_author_email ]['comment_count']++;
			}
		}

		$comment_email_group = wp_list_sort( $comment_email_group, 'comment_count', 'DESC', true );
		$comment_email_group = array_slice( $comment_email_group, 0, $number_commenters );

		$output = '';
		foreach ( $comment_email_group as $comment_author_email => $comment_meta ) {
			$output .= sprintf(
				'
  • %s
  • ', $comment_meta['comment_author_url'], $comment_meta['comment_author'], $comment_meta['comment_count'], get_avatar( $comment_author_email, 40 ), ); } add_option( 'syshut_commenter_list', $output ); } echo $output; }

    两个参数:$number_commenters 是要显示的读者头像数量,填写正整数即可,我这里用的 10$aftertime 是起始时间,代表统计某个时间点之后的评论,我这里是统计两年之内的,所以用的 '2 years ago'。第二个参数可用的时间关键字除了 year,还有 monthweekdayhoursecond 等。

    跟露兜的代码类似,我也可以排除指定类型的文章,例如代码中的 'has_password' => true, 表示不统计加密文章中的评论,其他排除方式用户可以根据需求自己扩展。'author__not_in' => get_the_author_meta( 'ID' ), 是排除评论者为当前文章作者的情况,当然也可以参照露兜的方式,排除所有的注册用户。避免重复大量查询数据库,上面代码把查询结果写入数据库,下次会直接从库调用。再配合下边这段代码使用:

    function syshut_clear_commenter_list_cache() {
    	delete_option( 'syshut_commenter_list' );
    }
    
    $work_tags = array(
    	'save_post',
    	'comment_post',
    	'wp_set_comment_status',
    );
    foreach ( $work_tags as $work_tag ) {
    	add_action( $work_tag, 'syshut_clear_commenter_list_cache' );
    }

    这样,在发布/更新文章发表评论或者评论状态改变时就会清空数据库,并在下次页面被访问时对数据库更新了。把以上两段代码贴到主题目录的 functions.php 中,再在侧边栏或其他打算显示的位置使用 syshut_commenter_list( 10, '2 years ago' ) 调用即可。看下效果:

    题外说一句,我这位近十年前在刚开博时对代码一窍不通的门外汉,现在居然也能东拼西凑写出一个完整的功能,不得不说 WordPress 强大无比,这也让我觉得自己还挺有成就感的。

    您可能还会对这些文章感兴趣
    评论总数 12(访客评论 7 / 作者评论 5)
    发表评论

    您的电子邮箱地址不会被公开。必填项已用 * 标注

    你好,新朋友!

    emojiemojiemojiemojiemojiemojiemojiemojiemojiemojiemojiemojiemojiemojiemojiemojiemojiemojiemojiemojiemojiemoji