您现在的位置是:首页>woredpress
woredpress
为WordPress文章作者评论留言显示“文本作者”提示
李陌阳2020-06-23 13:49:26woredpress280来源:水泊网络
今天在鸟哥博客看到这个教程,里面的一些评论者说这个功能还是挺不错的,仔细想了下确实可以拿来用下。
在文章内容评论者留言显示“文本作者”提示,可以让读者明确知道是作者亲自回复自己的留言应该还是挺有喜悦感的。不过这个适合多作者的博客网站,单一作者的博客站点建议还是用“管理员“提示更好些。
首先将下面判断文章作者代码添加到当前主题函数模板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; }
然后将显示调用的代码添加到主题评论模板显示评论者名称代码后面即可!
<?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,默认主题虽然外观看似简单,但功能真的很强大,有很多东西值得挖掘。
发表评论