最近访客
uuu1354134的头像-狐狸库
uuu1354134
百度未收录

WordPress最近评论显示头像的代码

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; }
温馨提示: 本文最后更新于2022/10/20 03:38:51。若文章内容或图片失效,请 留言联系站长反馈!
!
也想出现在这里? 联系我们
创意广告
© 版权声明
THE END
点赞0赞赏 分享
评论 抢沙发

请登录后发表评论

    请登录后查看评论内容