此代码在7.3上运行,非常有效,除非您的用户有两个角色:
add_filter( \'body_class\', function( $classes = \'\' ) {
$current_user = new \\WP_User(get_current_user_id());
$user_role = [\'administrator\', \'moderator\']; //just a test, but this is what it\'ll look like if it had 2 roles.
$classes = [];
$classes[] = \'role-\' . $user_role;
return $classes;
});
然后,你知道吗,同样的错误出现了。另外,您传递的字符串为
$classes
到您的匿名函数,其中作为过滤器显然需要一个数组。
改为执行以下操作:
add_filter( \'body_class\', function( $classes ) {
$current_user = wp_get_current_user();
foreach( $current_user->roles as $user_role ) {
$classes[] = \'role-\' . $user_role;
}
return $classes;
});