wordpress获取最新评论功能

十度 wordpress 2015年12月20日 收藏

wordpress主题开发过程中,经常需要使用到最新评论的数据来丰富网站得内容,wordpress教程网将为大家介绍wordpress最新评论获取的方法:
1、在你主题有的functions.php文件添加如下代码:


/**
 +----------------------------------------------------------
 * 最新评论
 +----------------------------------------------------------
 * @param string $outer  屏蔽的用户(比如管理员)
 * @param int $limit  返回评论条数
 +----------------------------------------------------------
 * @return string
 +----------------------------------------------------------
 */
function get_recent_comments($outer, $limit) {
	global $wpdb;
	$sql = "SELECT DISTINCT ID, post_title, post_password, comment_ID, comment_post_ID, comment_author, comment_date_gmt, comment_approved,comment_author_email, comment_type,comment_author_url, SUBSTRING(comment_content,1,40) AS com_excerpt 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 comment_author != '" . $outer . "' AND post_password = '' ORDER BY comment_date_gmt DESC LIMIT $limit";
	$comments = $wpdb->get_results ( $sql );
	foreach ( $comments as $comment ) {
		$output .= "\n<li><a href=\"" . get_permalink ( $comment->ID ) . "#comment-" . $comment->comment_ID . "\" title=\"" . strip_tags ( $comment->comment_author ) . ": ".strip_tags ( $comment->com_excerpt )."\"><em>&gt;</em>" . mb_strimwidth ( strip_tags ( $comment->com_excerpt ), 0, 36, '...' ) . "</a></li>";
	}
	echo $output;
}

2、在你需要调用到wordpress最新评论数据地方添加以下代码:

<ul class="comments-aside">
	<?php  get_recent_comments( 'shouce.ren' , '6' );?>
</ul>

3、刷新你的页面,应该就可以看到了。