在評論列表中,如果能給文章作者或是站長加上一個有趣的小標識,讓他們更加顯眼,不就很容易讓看評論的訪客抓住重點了。這一次的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,默認主題雖然外觀看似簡單,但功能真的很強大,有很多東西值得挖掘。