在評(píng)論列表中,如果能給文章作者或是站長(zhǎng)加上一個(gè)有趣的小標(biāo)識(shí),讓他們更加顯眼,不就很容易讓看評(píng)論的訪客抓住重點(diǎn)了。這一次的wordpress開(kāi)發(fā)教你怎么判斷當(dāng)前評(píng)論的是作者還是站長(zhǎng)。
- 代碼來(lái)源:詳情
首先將下面判斷文章作者代碼添加到當(dāng)前主題函數(shù)模板 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;
}
將顯示調(diào)用代碼添加到主題評(píng)論模板顯示評(píng)論者名稱(chēng)代碼的后面即可。
<?php
$post_author = zm_comment_by_post_author( $comment );
if ( $post_author ) {
echo '<span class="post-author">文章作者</span>';
}
?>
不同主題評(píng)論模板代碼不同,具體加到哪個(gè)位置,只能自行研究了。
同時(shí)顯示管理員和作者的調(diào)用方法:
<?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默認(rèn)主題Twenty Twenty,默認(rèn)主題雖然外觀看似簡(jiǎn)單,但功能真的很強(qiáng)大,有很多東西值得挖掘。