开关与IF语句组合在一起

时间:2019-03-29 作者:LovinQuaQua

我无法运行此功能。我注释掉的内容正在正常运行,但我想对特定的用户角色使用开关。。。

add_action( \'um_members_just_after_name\', \'my_members_after_user_name\', 10, 1 );
function my_members_after_user_name( $user_id ) {

    $user = new WP_User( $user_id );
    /*
    if( $user->roles[0] == \'um_musiker\' ) {
        // update_user_meta( $user_id, \'um_reihung\', \'40\' );
        echo \'<i class="um-verified um-icon-checkmark-circled um-tip-s" title="Kein Status"></i>\';  
    }
    */

    if( $user->roles[0] == $value ) {
        switch ( $value ) {
        case "um_musiker":
            echo \'<i class="um-verified um-icon-checkmark-circled um-tip-s" title="Kein Status"></i>\';
            break;
        case "um_musiker_bronze":
            echo \'<i class="um-verified um-icon-checkmark-circled um-tip-s" title="Bronze Status"></i>\';
            break;
        case "um_musiker_silber":
            echo \'<i class="um-verified um-icon-checkmark-circled um-tip-s" title="Silber Status"></i>\';
            break;
        }
    }
}

3 个回复
最合适的回答,由SO网友:Tejas Gajjar 整理而成

您不需要使用pass$value 在switch语句中,只需传递到那里$user->roles[0] 而不是$value 之后你的状况。

添加下面的代码,让我知道你对它的答案。

add_action( \'um_members_just_after_name\', \'my_members_after_user_name\', 10, 1 );
function my_members_after_user_name( $user_id ) {

    $user = new WP_User( $user_id );
    /*
    if( $user->roles[0] == \'um_musiker\' ) {
        // update_user_meta( $user_id, \'um_reihung\', \'40\' );
        echo \'<i class="um-verified um-icon-checkmark-circled um-tip-s" title="Kein Status"></i>\';  
    }
    */


        switch ( $user->roles[0] ) {
        case "um_musiker":
            echo \'<i class="um-verified um-icon-checkmark-circled um-tip-s" title="Kein Status"></i>\';
            break;
        case "um_musiker_bronze":
            echo \'<i class="um-verified um-icon-checkmark-circled um-tip-s" title="Bronze Status"></i>\';
            break;
        case "um_musiker_silber":
            echo \'<i class="um-verified um-icon-checkmark-circled um-tip-s" title="Silber Status"></i>\';
            break;
        }

}

SO网友:Jacob Peattie

正在检查$user->roles[0] 检查用户是否有角色的适当方法是否合适。用户可以有多个角色,因此需要考虑这一点:

$user  = get_userdata( $user_id );
$roles = $user->roles;

if ( in_array( \'um_musiker\', $roles ) {
    echo \'<i class="um-verified um-icon-checkmark-circled um-tip-s" title="Kein Status"></i>\';
    return;
}

if ( in_array( \'um_musiker_bronze\', $roles ) {
    echo \'<i class="um-verified um-icon-checkmark-circled um-tip-s" title="Bronze Status"></i>\';
    return;
}

if ( in_array( \'um_musiker_silber\', $roles ) {
    echo \'<i class="um-verified um-icon-checkmark-circled um-tip-s" title="Silber Status"></i>\';
    return;
}

SO网友:Rizwan Zakir

我只是写新的$value = $user->roles[0]; 变量和检查$user 在里面if 条件我希望这对你有帮助。谢谢

add_action( \'um_members_just_after_name\', \'my_members_after_user_name\', 10, 1 );
function my_members_after_user_name( $user_id ) {

  $user = new WP_User( $user_id );
  /*
  if( $user->roles[0] == \'um_musiker\' ) {
    // update_user_meta( $user_id, \'um_reihung\', \'40\' );
    echo \'<i class="um-verified um-icon-checkmark-circled um-tip-s" title="Kein Status"></i>\';  
  }
  */

  $value = $user->roles[0];

  if( $value ) {
    switch ( $value ) {
    case "um_musiker":
        echo \'<i class="um-verified um-icon-checkmark-circled um-tip-s" title="Kein Status"></i>\';
        break;
    case "um_musiker_bronze":
        echo \'<i class="um-verified um-icon-checkmark-circled um-tip-s" title="Bronze Status"></i>\';
        break;
    case "um_musiker_silber":
        echo \'<i class="um-verified um-icon-checkmark-circled um-tip-s" title="Silber Status"></i>\';
        break;
    }
  }
}

相关推荐

Wordpress switch domains

所以我在我的网站上使用cpanelpublic.html/site.com 文件夹中安装了一个wordpress。我创建了子域test.site.com 哪个根在publilc.html/test 我已经在那里创建了网站,现在我想把它转移到site.com. 我现在有两个问题。首先,我不知道怎么做,因为我尝试了很多事情,但都没有成功。第二,在尝试时,我打破了test 网站,现在我可以访问它,但中途。我的意思是我可以访问wp-admin 登录,但当我离开test.site.com 它将我重定向到site.c