1

我在 WordPress 中有一个 div,它显示了已登录用户的一些当前用户信息。问题是,如果信息不存在或该字段为空白,我希望不显示整行,例如某人中间名字。如果中间名不存在或为空,我希望它像这样显示。

名字:布拉

姓名:布拉哈斯

并不是

名字:布拉

中间名字:

姓名:布拉哈斯

我的代码是

<?php global $current_user;  get_currentuserinfo(); echo 'Relationship to Employee:' . $current_user->dep1_relationship . "\n";?><br>
<?php global $current_user;  get_currentuserinfo(); echo 'First Name:' . $current_user->dep1_firstname . "\n";?>
<?php global $current_user;  get_currentuserinfo(); echo 'Middle Name:' . $current_user->dep1_middlename . "\n";?>
<?php global $current_user;  get_currentuserinfo(); echo 'Last Name:' . $current_user->dep1_lastname . "\n";?><br>
<?php global $current_user;  get_currentuserinfo(); echo 'SSN:' . $current_user->dep1_social . "\n";?><br>
<?php global $current_user;  get_currentuserinfo(); echo 'Date of Birth:' . $current_user->dep1_dob . "\n";?><br>
<?php global $current_user;  get_currentuserinfo(); echo 'Gender:' . $current_user->dep1_gender . "\n";?><br>
<?php global $current_user;  get_currentuserinfo(); echo 'Student:' . $current_user->dep1_student . "\n";?><br>
<?php global $current_user;  get_currentuserinfo(); echo 'Add of Cancel:' . $current_user->dep1_addorcancelcoverage . "\n";?><br>
4

1 回答 1

1

尝试以下...

<?php 
global $current_user;  
get_currentuserinfo(); 

echo ($current_user->dep1_relationship!='')?'Relationship to Employee: '.$current_user->dep1_relationship.'<br />':"";
echo ($current_user->dep1_firstname!='')?'First Name: '.$current_user->dep1_firstname.'<br />':"";
echo ($current_user->dep1_middlename!='')?'Middle Name: '.$current_user->dep1_middlename.'<br />':"";
echo ($current_user->dep1_lastname!='')?'Last Name: '.$current_user->dep1_lastname.'<br />':"";
echo ($current_user->dep1_social!='')?'SSN: '.$current_user->dep1_social.'<br />':"";
echo ($current_user->dep1_dob!='')?'Date of Birth: '.$current_user->dep1_dob.'<br />':"";
echo ($current_user->dep1_gender!='')?'Gender: '.$current_user->dep1_gender.'<br />':"";
echo ($current_user->dep1_student!='')?'Student: '.$current_user->dep1_student.'<br />':"";
echo ($current_user->dep1_addorcancelcoverage!='')?'Add of Cancel: '.$current_user->dep1_addorcancelcoverage.'<br />':"";
?>

请注意,您只需要以下一次...

global $current_user;  
get_currentuserinfo(); 
于 2012-08-27T15:21:07.857 回答