下面是通过注释id返回注释作者姓名首字母的函数
function get_comment_author_initials($comment_id) {
$author = get_comment_author($comment_id);
$words = explode(" ", $author);
$initials = null;
foreach ($words as $w) {
$split = mb_str_split($w); // to cover unicode char
$initials .= $split[0];
}
return $initials;
}
function mb_str_split( $string ) {
return preg_split(\'/(?<!^)(?!$)/u\', $string );
}
您可以像这样在代码中使用它
<div class="class2">
<?php echo get_comment_author_initials($comment->comment_ID); ?>
</div>