华纳云香港服务器

WordPress文章作者回复留言时如何显示“作者”提示?

WordPress主题推荐

当 WordPress 文章作者在自己的文章中回复读者评论留言时,如果可以在评论昵称后面显示“作者”提示,可以让读者明确知道是作者亲自回复自己的评论留言是不是有点小鸡冻呢?这个功能比较适合多作者的博客网站,单一作者的博客还是建议使用网上盛传的“管理员“提示更好些。

1、首先将下面判断文章作者代码添加到当前主题函数模板 functions.php 中:

// 判断文章作者
function zm_comment_by_post_author( $comment = null ) {
if ( is_object( $comment ) && $comment->user_id - 0 ) {
$user = get_userdata( $comment->user_id );
$post = get_post( $comment->comment_post_ID );
if ( ! empty( $user ) && ! empty( $post ) ) {
return $comment->user_id === $post->post_author;
}
}
return false;
}

2、将显示调用代码添加到主题评论模板显示评论者名称代码的后面即可。

<?php
$post_author = zm_comment_by_post_author( $comment );
if ( $post_author ) {
echo '<span class="post-author">文章作者</span>';
}
?>
注意:不同主题评论模板代码不同,具体加到哪个位置,只能自行研究了。

同时显示管理员和作者的调用方法:

<?php 
if ($comment->comment_author_email == get_option('admin_email')) {
echo '<span class="author-admin">博主</span>';
} else {
$post_author = zm_comment_by_post_author( $comment );
if ( $post_author ) {
echo '<span class="post-author">作者</span>';
}
}
?>

代码取自 WordPress 默认主题 Twenty Twenty,默认主题虽然外观看似简单,但功能真的很强大,有很多东西值得挖掘。

以上内容整理自@知更鸟

本文地址:https://boke112.com/post/7658.html

赞 (0) 打赏
版权声明:本文为原创文章,版权归 boke112百科 所有,欢迎分享本文,转载请保留出处!发布此文是出于传递更多信息之目的,若有来源标注错误或侵犯了您的合法权益,请联系我们,确认后马上更正或删除,谢谢!
wu