小編給大家分享一下顯示WordPress某個文章所有評論者名稱的實現方法,希望大家閱讀完這篇文章后大所收獲,下面讓我們一起去探討吧!
使用場景,比如在文章適當位置,顯示當前已有:史珍香,秦壽生,焦厚根,朱逸群,夏建仁等發表了熱情揚溢的評論,再加一個錨點鏈接,引導讀者跳轉到評論表單,也發個熱情揚溢的評論。
將代碼添加到當前主題函數模板functions.php中:
function get_comment_authors_list( $id = 0, $sep = ', ' ) { $post_id = $id ? $id : get_the_ID(); if ( $post_id ) { $comments = get_comments( array( 'post_id' => $post_id, 'status' => 'approve', 'type' => 'comment', ) ); $names = array(); foreach ( $comments as $comment ) { $name = $comment->comment_author; if ( $comment->user_id ) { $user = get_userdata( $comment->user_id ); $name = $user ? $user->display_name : $name; } $arr = explode( ' ', trim( $name ) ); if ( ! empty( $arr[0] ) && ! in_array( $arr[0], $names ) ) { $names[] = $arr[0]; } } unset( $comments ); $sep = $sep ? $sep : ', '; return implode( $sep, $names ); } } add_shortcode( 'comment_authors_list', 'comment_authors_list_shortcode' ); function comment_authors_list_shortcode( $atts = array() ) { $atts = shortcode_atts( array( 'post_id' => 0, 'list_sep' => '', ), $atts ); return get_comment_authors_list( $atts['post_id'], $atts['list_sep'] ); }
使用方法:
一、調用ID為:123文章的所有評論者名稱
在模板中使用:
<?php echo get_comment_authors_list('123'); ?>
在文章添加短代碼:
[comment_authors_list post_id="123" /]
二、調用當前文章所有評論者名稱,與上面類似只是去掉其中的文章ID,適合放在文章正文模板中。
在模板中使用
<?php echo get_comment_authors_list(); ?>
在文章中添加短代碼:
[comment_authors_list /]
看完了這篇文章,相信你對顯示WordPress某個文章所有評論者名稱的實現方法有了一定的了解,想了解更多相關知識,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。