WordPress最近评论代码
WordPress自带的评论小工具,显示的是评论所在的文章,而且无gravatar头像,下面的代码可以显示最近评论者的gravatar头像和评论内容。
首先,将下面的代码添加到您当前的主题 functions.php 文件中
- function bg_recent_comments($no_comments = 5, $comment_len = 80, $avatar_size = 48) {
- $comments_query = new WP_Comment_Query();
- $comments = $comments_query->query( array( ‘number’ => $no_comments ) );
- $comm = ”;
- if ( $comments ) : foreach ( $comments as $comment ) :
- $comm .= ‘<li>’ . get_avatar( $comment->comment_author_email, $avatar_size );
- $comm .= ‘<a class=“author” href=“‘ . get_permalink( $comment->post_ID ) . ‘#comment-‘ . $comment->comment_ID . ‘”>’;
- $comm .= get_comment_author( $comment->comment_ID ) . ‘:</a> ‘;
- $comm .= ‘<p>’ . strip_tags( substr( apply_filters( ‘get_comment_text’, $comment->comment_content ), 0, $comment_len ) ) . ‘</p></li>’;
- endforeach; else :
- $comm .= ‘No comments.’;
- endif;
- echo $comm;
- }
可以修改上述代码第一行中的评论gravatar大小和评论显示的字数。
在WordPress主题你想要显示评论的位置添加如下代码:
- <div class=“widget recent-comments”>
- <h3>Recent Comments</h3>
- <?php bg_recent_comments(); ?>
- </div>
并添加CSS样式:
- .recent-comments { list-style: none; font-size: 12px; color: #485358; }
- .recent-comments li { overflow: hidden; padding: 20px 0; border-top: 1px dotted #DADEE1; }
- .recent-comments li:first-child { border: 0 none; }
- .recent-comments img { float: left; margin-right: 8px; }
- .recent-comments a { display: block; margin-top: 10px; padding-top: 10px; }