如何显示WordPress某个文章所有评论者名称
上面由WordPress教程栏目给大师引见显现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'] ); }
利用方式:
1、挪用ID为:123文章的一切批评者称号
正在模板中利用:
<?php echo get_comment_authors_list('123'); ?>
正在文章增加短代码:
[comment_authors_list post_id="123" /]
2、挪用当前文章一切批评者称号,取上里近似只是来失落此中的文章ID,合适放正在文章注释模板中。
正在模板中利用
<?php echo get_comment_authors_list(); ?>
正在文章中增加短代码:
[comment_authors_list /]
以上便是若何显现WordPress某个文章一切批评者称号的具体内容,更多请存眷酷吧易资本网别的相干文章!
【声明】 本网站所有发布资源;仅供学习和研究使用,您必须在下载后24小时内彻底删除。不得使用于商业用途,否则后果自负!
酷吧易(KUBAYI.COM) » 如何显示WordPress某个文章所有评论者名称
酷吧易(KUBAYI.COM) » 如何显示WordPress某个文章所有评论者名称