WordPress 默认评论表单的字段都只有昵称、邮箱、网址、内容等,可以根据自己的需要灵活的添加或删除自定义的字段呢,在首页文章列表直接显示文章的评论内容,可以用到分类页、标签页、日期归档页等效果,显得很高大尚参与互动人数很多,荒岛本次带来 WordPress 调用评论列表的教程,只要将下面的代码添加到主题的「functions.php」文件即可。宝藏来自荒岛 - 一座藏有宝藏的小岛-https://x-imagine.com/wordpress-call-comment-list.html
// 荒岛一座藏有宝藏的小岛 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
请按 Ctrl+D 收藏荒岛分享给好友 如您发现本文件已经失效无法下载请联系站长修正
- 荒岛公众号
- 扫一扫关注
- 荒岛小程序
- 扫一扫关注
评论