我在WordPress中有一个div,它显示了某些已登录用户的当前用户信息。问题是,如果信息不存在或字段为空,我希望整行不显示,比如某人的中间名。如果中间名不存在或为空,我希望它显示为这样。
名字:Blah
姓氏:Blahas
而非
名字:Blah
中间名:
姓氏:Blahas
我的代码是
<?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>
答案 0 :(得分: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();