WordPress 调用评论列表 WP 主题优化增强 function 文件实用功能代码段

ximagine
ximagine
管理
2014
文章
0
粉丝
教程 说明书评论372字数 434阅读1分26秒阅读模式
宝藏摘要WordPress默认评论表单的字段都只有昵称、邮箱、网址、内容等,可以根据自己的需要灵活的添加或删除自定义的字段呢,在首页文章列表直接显示文章的评论内容,可以用到分类页、标签页、...
荒岛广告位招商进行中...
WordPress 调用评论列表 WP 主题优化增强 function 文件实用功能代码段
日期:2023年9月2日 分类:教程  说明书 评论:发表评论 浏览:37

WordPress 默认评论表单的字段都只有昵称、邮箱、网址、内容等,可以根据自己的需要灵活的添加或删除自定义的字段呢,在首页文章列表直接显示文章的评论内容,可以用到分类页、标签页、日期归档页等效果,显得很高大尚参与互动人数很多,荒岛本次带来 WordPress 调用评论列表的教程,只要将下面的代码添加到主题的「functions.php」文件即可。

// 荒岛一座藏有宝藏的小岛
function get_comment_authors_list( $id = null ) {
	$post_id = $id ? $id : get_the_ID();
	if ( $post_id ) {
		$comments = get_comments( array(
			'post_id' => $post_id,
			'status'  => 'approve',
			'order' => 'ASC',
			'author__not_in' => get_the_author_meta('ID'),
			'type'    => 'comment',
		) );

		$names = array();
		foreach ( $comments as $comment ) {
			$arr = explode( ' ', trim( $comment->comment_author ) );
			if ( ! empty( $arr[0] ) && ! in_array( $arr[0], $names ) ) {
				$names[] = $arr[0];
			echo '<a class="names-scroll"><li>';
			if (get_option('cache_avatar')) {
				echo begin_avatar( $comment->comment_author_email, 96, '', get_comment_author( $comment->comment_ID ) );
			} else {
				echo get_avatar( $comment->comment_author_email, 96, '', get_comment_author( $comment->comment_ID ) );
			}
			echo get_comment_author( $comment->comment_ID );
			echo '</li></a>';
			}
		}
		unset( $comments );
	}
}

function qa_get_comment_last( $id = null ) {
	$post_id = $id ? $id : get_the_ID();
	if ( $post_id ) {
		$comments = get_comments( array(
			'post_id' => $post_id,
			'status'  => 'approve',
			'type'    => 'comment',
			'number' => '1',
		) );

		$names = array();
		foreach ( $comments as $comment ) {
			$arr = explode( ' ', trim( $comment->comment_author ) );
			if ( ! empty( $arr[0] ) && ! in_array( $arr[0], $names ) ) {
				$names[] = $arr[0];
				echo '<span class="qa-meta qa-last"><span class="qa-meta-class"></span>';
				echo '<a href="'.esc_url( get_permalink() ).'#comments"><span>' . sprintf(__( '最后回复', 'begin' )) . '<span class="qa-meta-class"></span>';
				echo '<span class="qa-meta-name">';
				echo get_comment_author( $comment->comment_ID );
				echo '</span>';
				echo '</span></a>';
				echo '</span>';
			}
		}
		unset( $comments );
	}
}
宝藏内容来自荒岛-https://x-imagine.com/wordpress-call-comment-list.html宝藏内容来自荒岛-https://x-imagine.com/wordpress-call-comment-list.html

请按 Ctrl+D 收藏荒岛分享给好友 如您发现本文件已经失效无法下载请联系站长修正

荒岛公众号
扫一扫关注
weinxin
HDsoft27
公众号ID已复制,前往微信关注...
荒岛小程序
扫一扫关注
weinxin
ximagine
哔哩姬ID已复制,前往B站关注...
荒岛广告位招商进行中...
 
荒岛广告位招商进行中...

发表评论