本文介绍如何按照Wordpress评论者的角色确定评论的显示样式,以及如何让评论的class更干净。
定义一个按照给定的用户ID返回其角色的函数
/*
Retrieve a commentator's role by the user ID
*/
function cwp_get_user_role( $user_id=null ){
if ( ! isset( $user_id ) ) return null;
$user = new WP_User( $user_id );
$wp_roles = new WP_Roles;
$role_names = $wp_roles->get_names();
$user_role_array = array ();
foreach ( $user->roles as $role ){
if ( isset ( $role_names[ $role ] ) )
$user_role_array[ $role ] = $role_names[ $role ];
}
return key( $user_role_array );
} |
/*
Retrieve a commentator's role by the user ID
*/
function cwp_get_user_role( $user_id=null ){
if ( ! isset( $user_id ) ) return null;
$user = new WP_User( $user_id );
$wp_roles = new WP_Roles;
$role_names = $wp_roles->get_names();
$user_role_array = array ();
foreach ( $user->roles as $role ){
if ( isset ( $role_names[ $role ] ) )
$user_role_array[ $role ] = $role_names[ $role ];
}
return key( $user_role_array );
}
为WORDPRESS的COMMENT_CLASS 添加过滤器
/*
Add filter to comment_class for your WordPress
*/
function cwp_add_comment_author_roles_to_comment_class( $classes, $class, $comment_id, $post_id ){
//Clean out the unnecessary classes for your comment list item.
//if you want to use it,uncomment the following sentence.
/* $classes=cwp_clean_out_unnecessary_comment_classes($classes);*/
$comment = get_comment( $comment_id );
$user_id = (int)$comment->user_id;
if ( 0 != $user_id ){
$role = cwp_get_user_role( $user_id );
if ( !isset( $role ) || empty( $role ) ) return $classes;
$classes[] = $role;
}
return $classes;
}
add_filter( 'comment_class', 'cwp_add_comment_author_roles_to_comment_class', 10, 4 ); |
/*
Add filter to comment_class for your WordPress
*/
function cwp_add_comment_author_roles_to_comment_class( $classes, $class, $comment_id, $post_id ){
//Clean out the unnecessary classes for your comment list item.
//if you want to use it,uncomment the following sentence.
/* $classes=cwp_clean_out_unnecessary_comment_classes($classes);*/
$comment = get_comment( $comment_id );
$user_id = (int)$comment->user_id;
if ( 0 != $user_id ){
$role = cwp_get_user_role( $user_id );
if ( !isset( $role ) || empty( $role ) ) return $classes;
$classes[] = $role;
}
return $classes;
}
add_filter( 'comment_class', 'cwp_add_comment_author_roles_to_comment_class', 10, 4 );
现在看看HTML输出情况:
<li class="comment byuser comment-author-pw even thread-odd thread-alt depth-1 contributor" id="comment-18">
<!--此评论的内容部分-->
</li> |
<li class="comment byuser comment-author-pw even thread-odd thread-alt depth-1 contributor" id="comment-18">
<!--此评论的内容部分-->
</li>
看到了吧,代表角色的”contributor“已经按照预想正确输出了,但是comment_class
默认输出了很多classes,但是并不是都有用的,下面定义一个清理函数,让清理过的classes仅保留comment:
/*
Clean out the unnecessary classes for your comment list item.
*/
function cwp_clean_out_unnecessary_comment_classes($classes){
foreach( $classes as $key => $class ) {
switch( $class ) {
case 'comment':
continue;
default:
unset( $classes[$key] );
continue;
break;
}
}
unset($key,$class);
return $classes;
} |
/*
Clean out the unnecessary classes for your comment list item.
*/
function cwp_clean_out_unnecessary_comment_classes($classes){
foreach( $classes as $key => $class ) {
switch( $class ) {
case 'comment':
continue;
default:
unset( $classes[$key] );
continue;
break;
}
}
unset($key,$class);
return $classes;
}
添加了上述代码后,再在’cwp_add_comment_author_roles_to_comment_class
‘函数中按照说明将被注释掉的那一句启用,然后再看看输出了什么:
<li class="comment contributor" id="comment-18">
<!--the comment item content is here-->
</li> |
<li class="comment contributor" id="comment-18">
<!--the comment item content is here-->
</li>
剩下要做的事情就是根据上述输出的代表角色的class来定义css了,这个,得你自己按照需要来做。
来源:
https://www.wpdaxue.com/styling-wordpress-comments-based-on-user-roles.html
微信公众号
手机浏览(小程序)
Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages:
error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in
/mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line
57
Warning: get_headers(): Failed to enable crypto in
/mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line
57
Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_32221.jpg): failed to open stream: operation failed in
/mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line
57