
在评论列表中,如果能给文章作者或是站长加上一个有趣的小标识,让他们更加显眼,不就很容易让看评论的访客抓住重点了。这一次的 WordPress 开发教你怎么判断当前评论的是作者还是站长。
- 代码来源:详情
首先将下面判断文章作者代码添加到当前主题函数模板 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,默认主题虽然外观看似简单,但功能真的很强大,有很多东西值得挖掘。