大峰笔记
  • 关于
  • 搜索

Wordpress首页显示最近的评论者头像 Sun, Jun 23, 2024

代码如下,加入到合适位置。

<?php
// 获取最新的25条评论,排除作者的头像
$args = array(
    'number'     => 25,// 获取最新的25条评论
    'type'       => 'comment',// 获取评论类型
    'status'     => 'approve',// 只获取已经被批准的评论
    'author__not_in' => array(1), // 排除博主的ID,这里假设博主ID是1
    'order'      => 'DESC'// 按照评论时间降序排列
);
 
$comments = get_comments($args);
 
// 循环显示评论者的头像
foreach ($comments as $comment) :
    // 排除作者的头像
    if ($comment->comment_author != $comment->comment_author_email ) :    
        // 显示头像
         echo '<a href="' . get_comment_link($comment, array('type' => 'comment')) . '">';
            echo get_avatar($comment, 64); // 显示评论者的头像,尺寸为64px
            echo '</a>';
    endif;
endforeach;
?>    

css中添加以下,配合主题宽度。

.avatar{width: 34px;height: 34px;}

注意:代码的标点符号大小写,如有问题,把引号符号改成小写!!!

直接显示来访者相应博客地址

69,// 获取最新的25条评论 ‘type’ => ‘comment’,// 获取评论类型 ‘status’ => ‘approve’,// 只获取已经被批准的评论 ‘author__not_in’ => array(1), // 排除博主的ID,这里假设博主ID是1 ‘order’ => ‘DESC’// 按照评论时间降序排列 ); $comments = get_comments($args); // 循环显示评论者的头像 foreach ($comments as $comment) : // 排除作者的头像 if ($comment->comment_author != $comment->comment_author_email ) : // 获取评论者的网址,如果没有则为空字符串 $comment_author_url = get_comment_author_url($comment); // 显示头像 echo ‘’; echo get_avatar($comment, 64); // 显示评论者的头像,尺寸为64px echo ‘’; endif; endforeach; ?>

直接显示来访者相应博客地址,并把地址用base64显示

69,// 获取最新的25条评论 ‘type’ => ‘comment’,// 获取评论类型 ‘status’ => ‘approve’,// 只获取已经被批准的评论 ‘author__not_in’ => array(1), // 排除博主的ID,这里假设博主ID是1 ‘order’ => ‘DESC’// 按照评论时间降序排列 ); $comments = get_comments($args); // 循环显示评论者的头像 foreach ($comments as $comment) : // 排除作者的头像 if ($comment->comment_author != $comment->comment_author_email ) : // 获取评论者的网址,如果没有则为空字符串 $comment_author_url = ( $comment->comment_author_url ); // 显示头像 echo ‘’; echo get_avatar($comment, 64); // 显示评论者的头像,尺寸为64px echo ‘’; endif; endforeach; ?>

20241018更新:直接显示所有来访者相应博客地址,并把地址用base64显示,不重复显示头像。(如果有网址,头像将被包裹在一个链接中;如果没有网址,头像则直接通过get_avatar()函数显示,没有链接。)

<?php
// 获取所有评论并按照评论ID排序
$comments = get_comments(array(
    'status' => 'approve', // 只获取已经被批准的留言
    'order' => 'DESC', // 倒序排列
    'author__not_in' => array(1), // 排除博主的ID,这里假设博主ID是1
));
// 用来存储不重复的邮箱
$unique_emails = array();
// 遍历评论
foreach ($comments as $comment) {
    // 检查邮箱是否已经存在于数组中
    if (!isset($unique_emails[$comment->comment_author_email])) {
        // 如果不存在,记录邮箱
        $unique_emails[$comment->comment_author_email] = true;
        // 获取评论者的网址
        $comment_author_url = $comment->comment_author_url;
        // 检查留言者是否填写了网址
        if (!empty($comment_author_url)) {
            // 显示头像及跳转链接
            echo '<a rel="nofollow" target="_blank" href="' . home_url() . "/goto?url=" . base64_encode($comment_author_url) . '">';
            echo get_avatar($comment, 64); // 显示评论者的头像,尺寸为64px
            echo '</a>';
        } else {
            // 显示没有网址的头像,不链接
            echo get_avatar($comment, 64); // 显示评论者的头像,尺寸为64px
        }
    }
}
?>

较新的一篇: wordpress拉取说说页面的最新一条标题到首页
较旧的一篇: 调用rss的第一条

Back to Home | bXpmOTk5QHFxLmNvbQ== | TOP ↑
hugo.386 | © 2025 | 苏ICP备2022047275号-1,苏公网安备 32020602001142号