如果用户不存在,则模板应返回一些信息,说明用户不存在,而不是尝试回显其数据,例如:
<?php $keyuser = get_user_by(\'login\', $username); ?>
<?php if(empty($keyuser)): ?>
<p>That username does not exist, please check for typos or try a different search cryteria.</p>
<?php else: ?>
<table>
<tr>
<th>User ID</th>
<th>Username</th>
<th>Email Address</th>
<th>Roles</th>
</tr>
<tr>
<td><?php echo $keyuser->ID; ?></td>
<td><?php echo $keyuser->user_login; ?></td>
<td><?php echo $keyuser->user_email; ?></td>
<td><?php echo $keyuser->roles[0]; ?></td>
</tr>
</table>
<?php endif; ?>
如果这不可行,并且需要将变量设置为空白字符串,那么可以使用
ternary if statement 如果你觉得这样看起来更整洁/减少打字
$keyuser = get_user_by(\'login\', $username);
$byusername_dename = $keyuser->user_login ?: \'\';
$byusername_deemail = $keyuser->user_email ?: \'\';
$byusername_hisrole = $keyuser->roles[0] ?: \'\';
$byusername_id = $keyuser->ID ?: \'\';