當WordPress文章作者在自己的文章中回復讀者評論留言時,在名稱后面顯示“文本作者”提示,可以讓讀者明確知道是作者親自回復自己的留言是不是有點小雞凍呢。
適合多作者的博客網站,單一作者的博客,還是用網上盛傳的“管理員“提示更好些。
首先將下面判斷文章作者代碼添加到當前主題函數模板functions.php中:
/**
* 檢查指定的評論是否由評論文章的作者撰寫。
*
*/
function twentytwenty_is_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 = twentytwenty_is_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 = twentytwenty_is_comment_by_post_author( $comment );
if ( $post_author ) {
echo '<span class="post-author">作者</span>';
}
}
?>判斷作者代碼取自WordPress默認主題Twenty Twenty,默認主題雖然外觀看似簡單,但功能真的很強大,有很多東西值得挖掘。






