我正在尝试在WordPress中显示基于用户名的消息。我使用的代码如下:
$current_user = wp_get_current_user();
if( 'user1' && 'user2' == $current_user->user_login ){
echo "discount 20%";
}
else {
echo "you could save 20% if you are a VIP customer";
}如果我将上述代码与&&运算符一起使用,则只对user2显示discount 20%
如果我将上述代码与||操作符一起使用,即使用户未登录,也会向所有用户显示discount 20%。
使用||编写代码
$current_user = wp_get_current_user();
if( 'user1' || 'user2' == $current_user->user_login ){
echo "discount 20%";
}
else {
echo "you could save 20% if you are a VIP customer";
}我会不断地添加用户名。折扣20%应该只显示我将添加的用户名。它不应显示给其他用户或访客/非登录用户。
我在这里做错了什么?
发布于 2019-07-01 15:41:19
如果您将来要添加用户名,下面的代码将非常有用。
// List of VIP usernames.
$vip_usernames = array(
'user1',
'user2'
);
$current_user = wp_get_current_user();
// Check if the username is in VIP usernames array.
if ( in_array( $current_user->user_login, $vip_usernames, true ) ) {
echo "discount 20%";
} else {
echo "you could save 20% if you are a VIP customer";
}https://stackoverflow.com/questions/56831564
复制相似问题